Finding the tile to your right

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Finding the tile to your right

Post 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 Image.
(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.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Finding the tile to your right

Post 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]
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: Finding the tile to your right

Post by toaster468 »

What does this return? The tile number? like 0, 1, 2, 3 ect.?
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Finding the tile to your right

Post 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?
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: Finding the tile to your right

Post by toaster468 »

-snip-
Last edited by toaster468 on Tue Oct 25, 2011 1:29 am, edited 1 time in total.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: Finding the tile to your right

Post by toaster468 »

Image

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
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Finding the tile to your right

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Finding the tile to your right

Post 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.
Help us help you: attach a .love.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: Finding the tile to your right

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests