Having an issue with grid-based movement in Love2D/Lua -


using love2d lua framework, trying program basic game nintendo-era rpgs heroes' , npcs' movement restricted tiled grid. far i've found past problems, until hit tricky error player movement isn't functioning correctly.

function love.load()     love.graphics.setdefaultfilter('nearest', 'nearest', 1)     love.keyboard.setkeyrepeat(true)     font = love.graphics.newfont(14) -- number denotes font size      win_size = 6     love.window.setmode(160 * win_size, 144 * win_size)      true_sfc = love.graphics.newcanvas(160,144)     view_sfc = love.graphics.newcanvas(160 * win_size, 144 * win_size)      player = {         grid_x = 3,         grid_y = 3,         act_x = 48,         act_y = 48,         transit = false,         direction = {0, 0}     } end  function love.update(dt)     if player.transit == true          -- idea if player selects direction, player walk in direction until stops on grid.          -- when press left or right, movements works intended- player square walks until stops on grid.         -- however, when press or down, player moves single pixel, despite having same instructions           player.act_x = player.act_x + player.direction[1]         player.act_y = player.act_y + player.direction[2]          if player.act_x == player.grid_x * 16              player.direction[1] = 0             player.transit = false         end          if player.act_y == player.grid_y * 16              player.direction[2] = 0             player.transit = false         end          -- in if-statement, if have program compare player's y-coordinates before comparing x coordinates,         -- program move player on y-axis correctly, locking 16 pixel grid, while x coordinates         -- starts have single-pixel movement issue.      end end  function love.draw()     love.graphics.setcanvas(true_sfc)         love.graphics.setcolor( 0, 0, 0)         love.graphics.rectangle("fill", 0, 0, 256, 224)         love.graphics.setcolor(255,255,255)         love.graphics.rectangle("fill", player.act_x, player.act_y, 16, 16)         love.graphics.print(player.direction[1], 100, 100)         love.graphics.print(player.direction[2], 100, 120)     love.graphics.setcanvas()      love.graphics.draw(true_sfc, 0, 0, 0, win_size, win_size) end  function love.keypressed(key)     if player.transit == false         if key == "up"             player.grid_y = player.grid_y - 1             player.direction = {0, -1}             player.transit = true         elseif key == "down"             player.grid_y = player.grid_y + 1             -- press down, player's map position goes down 1 tile             player.direction = {0, 1}             player.transit = true         elseif key == "left"             player.grid_x = player.grid_x - 1             player.direction = {-1, 0}             player.transit = true         elseif key == "right"             player.grid_x = player.grid_x + 1             player.direction = {1, 0}             player.transit = true         end     end end 

admittedly i'm pretty new lua don't understand how uses variables well. , realize code isn't effecient, that's planned on improving on time anyway.

the problem here check if vertical movement lines up, see does, , set self.transit false, preventing future checks.

you want check moving in direction, before checking if you're lined up:

    if player.direction[1] ~= 0 , player.act_x == player.grid_x * 16          player.direction[1] = 0         player.transit = false     end      if player.direction[2] ~= 0 , player.act_y == player.grid_y * 16          player.direction[2] = 0         player.transit = false     end 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -