jumping tutorial?

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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: jumping tutorial?

Post by kikito »

toaster468 wrote:OK, so I think I've found that I haven't asked it to move :crazy:. So here is my code for drawing the map:

Code: Select all

function draw_map(mx, my)

   for y=1, map_display_h do
      for x=1, map_display_w do
         love.graphics.draw(tile[ map[y] [x] ], x*tile_w, y*tile_h)
      end
   end
end
What do I do now?
You have to start using map2world and worl2map.
  • Include map2world and worl2map in your code, before draw_map (just copy and paste the two functions somewhere before defining draw_map
  • Modify draw_map so it uses map2world. You can do this by replacing x*tile_w, y*tile_h with map2world(x,y).
  • Test it. It should work as before.
If you get in trouble, my walls post has one example about how to do this change (below "Hint 2")

Let me know when it works.
When I write def I mean function.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: jumping tutorial?

Post by toaster468 »

OK, it compiled and it is exactly as it is before.

Next step please.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: jumping tutorial?

Post by kikito »

Ok next step is using world2map.

World2map is a function. If you give it coordinates in "world space" (such as the player's x and y) it will give you the map coordinates of the cell they occupy.

So if you want to know in what tile the player is, you can now do this:

Code: Select all

local tileX, tileY = world2map( player_X, player_Y )
With that information, it is quite easy to know the coordinates of the tile to the left of the player.

Code: Select all

local leftTileX, leftTileY = tileX - 1, tileY
If you want to know if there is a 'solid tile' on the left, you can do this:

Code: Select all

local leftTileNo = map[leftTileY][leftTileX] -- will return 0,1,2,3 etc (remember, 'y' goes first when using the map variable)
So your exercise now is changing the function that modifies the player position (depending on your implementation, love.update or love.keypress) so it checks whether the player can move to the left or right:
  • Start by calculating tileX, tileY, leftTileX, leftTileY and leftTileNo as shown above.
  • Then calculate the coordinates of the cell to the right of the player (rightTileX, rightTileY) and the number inside that cell (rightTileNo) (Use the calculations of the left tile as a model)
  • Once you have leftTileNo and rightTileNo, use them to limit your player movement to the left and right. If you are saying "If the left key is pressed, decrement player_X", now you will have to say "If the left key is pressed and the left tile is air, decrement player_X". And the same for the right.
The "left tile is air" will depend on how you code your tiles. If you have assigned the number '0' to traversable tiles, then you have to compare leftTileNo with 0.

Once you have done all this, your player should (hopefully) be unable to traverse walls horizontally.
When I write def I mean function.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: jumping tutorial?

Post by toaster468 »

I am getting an error:
Image

I do not know how to fix this one. :(
Last edited by toaster468 on Thu Dec 02, 2010 9:25 pm, edited 1 time in total.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: jumping tutorial?

Post by toaster468 »

Oh and I made up my own little equation to figure out what tile he is in :cool:.

Code: Select all

tile_X, tile_Y = (map_w - player_X)/2, (map_h - player_Y)/2
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: jumping tutorial?

Post by Robin »

toaster468 wrote:I am getting an error (...)
I do not know how to fix this one. :(
OK. Please go to line 69 and explain in your own words what the line does*. It might also be useful to copy the function that line's in and paste it here, along with an indication of what line is line 69.

* That explaining what it does won't help us help you, but it will help you help yourself. ;)
Help us help you: attach a .love.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: jumping tutorial?

Post by toaster468 »

This is in love:load()

Code: Select all

Line 68: leftTileX, leftTileY = tile_X - 1, tile_Y
used to get the tile to the left of the player.

Code: Select all

Line 69: leftTileNo = map[leftTileY][leftTileX] -- will return 0,1,2,3 etc (remember, 'y' goes first when using the map variable)

Looks to see if there is a tile to the left of the player, returns 0 or 1 (those are all the tiles in my game ATM.

This is in love:update(dt)
Then in my keypress event:

Code: Select all

if love.keyboard.isDown( "left" ) and leftTileNo == 0 then
	player_X = player_X - 1
	player_direction = "l"

	end
end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: jumping tutorial?

Post by bartbes »

My guess is, you're at the leftmost tile.
The thing is, the error means you are indexing something that is not defined, because this might seem vague, let me write you some code:

Code: Select all

t = { apple = { color = "green" }, invisible_pear = { color = "invisible"} }
this would mean that t.apple (equivalent to t["apple"]) is now the table containing color, so you could do

Code: Select all

print("Apple color is: " .. t.apple.color)
However, say you happen to want to get the color of an orange

Code: Select all

print("Orange color is: " .. t.orange.color)
This would fail, because you're indexing a nil value. Now, why is this indexing a nil value? Because t.orange hasn't been set, it's nil.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: jumping tutorial?

Post by toaster468 »

So what I should really do is:

Code: Select all

map[leftTileY][leftTileX] = tile_Y, tile_X - 1
?

EDIT: Wait the X and Y is switched. *fixed*
Last edited by toaster468 on Thu Dec 02, 2010 10:43 pm, edited 1 time in total.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: jumping tutorial?

Post by thelinx »

The problem seems to be that map[leftTileY] doesn't exist, so when you try to index [leftTileX] of that, you get a "indexing nil" error.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests