Collision with Tiles.
Now this project causes me trouble. I am trying to implement a collision check into my game, that works the following.
I have a player file, which has a dir, that is being set by the tileHandler. The tileHandler checks for pressed buttons, and updates positions of itself.
My Idea was, that the player check in a for loop, if a tile with the tag "wall", is exactly 16 pixels away from it.
But that doesn't seem to work, no matter what.
If someone is interested, here is the project link:
Not easy to follow the thought process of someone else's program flow, but here how I got it somehow to work:
- One spelling mistake, in 'tilehandler' you called it
player.coltop, in 'player.update' you spelled it
, I went with
- About your player.update check. By what I understand you go through the whole tile list and set
coltop if it sees a "wall".
Problem, it doesn't stop once it finds a wall and continuous to go through the list changing coltop back to 'not found' ... ?
- I used a different approach. Ok, quick tinker, there will be better ways. See below:
For the 'top' collision check, all we are interested in is the tile at x==0, y==16 and tile.tag=="wall". Once we find it we set coltop to true and
break out of the search loop, no need to look any further.
Here how I did it:
for tile in tiles
this.coltop = false
if tile.y==16 and tile.x==0 and tile.tag=="wall"then
this.coltop = true
Hope this helps. As said, there are better ways of doing it. In other examples you will see that many use a 'camera' object that is attached to the player to show the relevant area of the map. But shifting the whole map works by what I see, never thought of it :)
Thanks for that. I was probably to stoopid for this.
And to keep the resource usage low, I just stopped rendering tiles offscreen. Saved me about 40% of cpu load.
Nothing stupid at all, just a different approach. Actually gave me some new ideas ;)
Regarding the cpu load. At the moment you move and draw each tile one by one, for a visible screen area of lets say 20x20 that would be 400 draw calls.
Now, if you would create a custom image ( API - Image ) and draw the tiles into it during creation you would only have to keep track of one object to draw!
I would still keep the tile array though for collision check etc.
So instead of shifting 400 objects and drawing them you would only shift one.
On the other hand ... why not just using maps? In a way you create your own map draw variation.
Live Long and Tinker
I will try doing the map way.