Page 1 of 1
Finding the tile to your right
Posted: Tue Oct 25, 2011 1:02 am
by toaster468
I am making a game and it is going pretty good, but I have one part left to work on, collisions. I am using the
Tile Scrolling tutorial's method of displaying the tiles. I need to find the tile to the right of the player
.
(note the red is the player, and the yellow are the walls)
You guys might remember me from last year, I got a little better, and I am starting smaller so hopefully this one will work.
Thanks in advance.
Re: Finding the tile to your right
Posted: Tue Oct 25, 2011 1:12 am
by Taehl
The code for that might be (I have to guess, since you didn't post a .love)
Code: Select all
local tileToRight = map[player.x+1] and map[player.x+1][player.y]
Re: Finding the tile to your right
Posted: Tue Oct 25, 2011 1:13 am
by toaster468
What does this return? The tile number? like 0, 1, 2, 3 ect.?
Re: Finding the tile to your right
Posted: Tue Oct 25, 2011 1:17 am
by Jasoco
Yeah. Here, create a function to return the value of a tile easily:
Code: Select all
function checkTile(x, y)
return map[x][y]
end
Where map is whatever your array is called etc...
And then call it like so for Left, Right, Up and Down:
Code: Select all
checkTile(x-1, y) --Left
checkTile(x+1, y) --Right
checkTile(x, y-1) --Up
checkTile(x, y+1) --Down
Where x and y are the points you want to chedk. If the player is x, y then left would be x-1, right x+1, up y-1 and down y+1.
It will return whatever data you have stored in that tile. Like if you use 0 or 1 for collisions or whatever. Maybe 2 for water, 3 for lava, etc, etc...
Get it?
Re: Finding the tile to your right
Posted: Tue Oct 25, 2011 1:20 am
by toaster468
-snip-
Re: Finding the tile to your right
Posted: Tue Oct 25, 2011 1:25 am
by toaster468
I am getting this error. This is my code:
Code: Select all
if tonumber(checkTile(map_x+1, bity)) == "1" then
love.event.push("q")
end
Or alternatively:
http://dl.dropbox.com/u/27844356/bitdod ... 0Copy.love
Re: Finding the tile to your right
Posted: Tue Oct 25, 2011 1:33 am
by slime
The checktile function (and maybe the rest of your code) needs to account for when you input an x,y coordinate that's not within the bounds of the tile arrays, e.g.
Code: Select all
function checkTile(x, y)
if map[x] and map[x][y] then
return map[x][y]
else
return "cheater!"
end
end
or whatever.
Re: Finding the tile to your right
Posted: Tue Oct 25, 2011 5:26 am
by Robin
Also, the following code is wrong:
Code: Select all
if tonumber(checkTile(map_x+1, bity)) == "1" then
love.event.push("q")
end
Strings are not equal to numbers, so that expression will never return true.
Fix:
Code: Select all
if checkTile(map_x+1, bity) == 1 then
love.event.push("q")
end
That still doesn't fix your crash, but slime's suggestion will probably help.
Re: Finding the tile to your right
Posted: Wed Oct 26, 2011 12:15 am
by toaster468
I am not even getting any reaction from the program. As far as I know I am using the code the right way, but nothing is happening.
Newest code:
http://dl.dropbox.com/u/27844356/bitdod ... 82%29.love