# best way to collide with different tiles?

#### Dubbsta

##### Active Member
im trying different methods to collide with different tiles. so far i tried lists and it worked. i made 6 different size rects and was able to collide with all 6 with one call, but it doesnt seem right to use lilst on an entire level, thats a ton of addlast calls.

This time im trying map[][] =[[0,0,0], arrays where i would try to coll. with 1 and 2 with this call...............................[1,2,0]]

in the main class to get p1 to collide with specific tile images. but it seems like x,y have no real position i think.
i tried different variations of this code and nothing. can someone point me in the right direction. i looked a few places. also what different methods are there. thanks!

Code:
``````For Local i:= Eachin lvl.map
For Local j:= eachin lvl.map

If lvl.map[i][j] = 1 or lvl.map[i][j] = 2

If p1.x + p1.width >= lvl.x+j and p1.x <= lvl.x+j + lvl.tileSize and

p1.y + p1.height >= lvl.y+i and p1.y <= lvl.y+i + lvl.tileSize

p1.fallspd = 0

End
End
Next
next``````

#### Holzchopf

##### Moderator
but it seems like x,y have no real position i think.
You're thinking in the right direction already ;-)

The problem is, that this
Code:
``For Local i:= Eachin lvl.map``
will iterate through the whole -whatever lvl.map is-. In this case I guess it will iterate through an array of arrays of ints (an array whose elements are arrays whose elements are ints) and therefore i will be of type array<int> - not like you might expect indices going from 0 to the dimension of map -1. The same applies to j btw, since the eachin statement is the same in both cases.

If you really wanted through the complete map data, the head of your nested loops should look something like this:
Code:
``````For Local i:= 0 Until lvl.width
For Local j:= 0 Until lvl.height``````
With lvl.height and lvl.width being the dimensions (in tiles) of your map.

BUT:

If the purpose of your code is just to - lemmeguess - stop p1 from falling when hitting solid ground, then you don't have to check against collision with the whole level. Just check the tiles around p1.x and p1.y

#### Dubbsta

##### Active Member
• For Local i:= 0 Until lvl.width
• For Local j:= 0 Until lvl.height
thanks Holzchopf! that was actually what i did first haha but must've done it wrong obvi.

If the purpose of your code is just to - lemmeguess - stop p1 from falling when hitting solid ground, then you don't have to check against collision with the whole level. Just check the tiles around p1.x and p1.y
this seems like a much better and easier solution !...ummm could you lend me some pseudo on that, not sure how to implement that.
i do want to collide with different tiles and tileheights and occasional walls. a small platformer. ill keep trying!!!