Page 11 of 14
Re: jumping tutorial?
Posted: Wed Dec 01, 2010 9:17 am
by kikito
toaster468 wrote:OK, so I think I've found that I haven't asked it to move
. 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.
Re: jumping tutorial?
Posted: Wed Dec 01, 2010 10:28 pm
by toaster468
OK, it compiled and it is exactly as it is before.
Next step please.
Re: jumping tutorial?
Posted: Thu Dec 02, 2010 7:54 am
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.
Re: jumping tutorial?
Posted: Thu Dec 02, 2010 9:20 pm
by toaster468
I am getting an error:
I do not know how to fix this one.
Re: jumping tutorial?
Posted: Thu Dec 02, 2010 9:21 pm
by toaster468
Oh and I made up my own little equation to figure out what tile he is in
.
Code: Select all
tile_X, tile_Y = (map_w - player_X)/2, (map_h - player_Y)/2
Re: jumping tutorial?
Posted: Thu Dec 02, 2010 9:51 pm
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.
Re: jumping tutorial?
Posted: Thu Dec 02, 2010 9:59 pm
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
Re: jumping tutorial?
Posted: Thu Dec 02, 2010 10:08 pm
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.
Re: jumping tutorial?
Posted: Thu Dec 02, 2010 10:40 pm
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*
Re: jumping tutorial?
Posted: Thu Dec 02, 2010 10:43 pm
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.