Although I've read few tutorials on Lua and I have an idea in my head about how to use Lua's for loops, I'm having trouble. Maybe it's because I'm used to Python's for-loops? Whenever the player moves in my game, the game checks to see if a wall on the map is in the way, and it checks to see if an NPC is in the destination's coordinates.
While I got wall-checking to function perfectly, this new NPC for-loop makes it so the player never moves. The checkSpace function looks through a list of NPCs (which there is only one currently) to see if the player can move to the destination.
player = {
grid_x = 2,
grid_y = 2,
act_x = 32,
act_y = 32,
transit = false,
direction = {0, 0}
}
npc = {
grid_x = 4,
grid_y = 3,
act_x = 64,
act_y = 48,
}
npcs = {npc}
function checkSpace(grid_y, grid_x, direction)
if map[grid_y + (1 + direction[2])][grid_x + (1 + direction[1])] == 0 then
-- if checkNPCs
for _,v in pairs(npcs) do
if grid_x == v.grid_x and grid_y == v.grid_y then
return true
end
end
end
end
function love.keypressed(key)
if player.transit == false then
if key == "up" then
player.direction = {0, -1}
if checkSpace(player.grid_y, player.grid_x, player.direction) == true then
player.grid_y = player.grid_y - 1
-- move player.direction before the if statement to make the player change direction whether or no there is space to move
player.transit = true
end
end
end
end
Edit: I fiddled around with the program a bit and I've made some progress. Instead of the keypressed program checking to see if checkSpace returns True, the programs been modified so that it returns false if there isn't an obstacle in the way.
if key == "up" then
player.direction = {0, -1}
if checkSpace(player.grid_y, player.grid_x, player.direction) == false then
player.grid_y = player.grid_y - 1
-- move player.direction before the if statement to make the player change direction whether or no there is space to move
player.transit = true
end
I've gotten a very basic (and practically useless) for-loop working with my program, but if I try to do anything more advanced with it then I get the error where my player character will not move.
for nameCount = 1, 1 do
if grid_x + direction[1] == npc.grid_x and grid_y + direction[2] == npc.grid_y then
return true
else
return false
end
end
I've posted the full code at this location: http://pastebin.com/QNpAU6yi
checkSpacealways returntrue? - Yu Hao