Discord
Login
Community
DARK THEME

Why my player doesn't move

this is my code :

checkCollision = function(r1, r2)
  return r1.x - r1.lar/2 < r2.x + r2.lar/2 and
         r1.x + r1.lar/2 > r2.x - r2.lar/2 and
         r1.y - r1.lon/2 < r2.y + r2.lon/2 and
         r1.y + r1.lon/2 > r2.y - r2.lon/2
end

initPlayer = function()
  idle = true
  player = object
    x = 0
    y = 0
    lon = 50
    lar = 50
    vitesse = 4
    vie = 5
    age = 0
    mouvement = true
  end
end

updatePlayer = function()
  local dx = 0
  local dy = 0

  if player.mouvement then
    if keyboard.UP then dy += player.vitesse end
    if keyboard.DOWN then dy -= player.vitesse end
    if keyboard.LEFT then dx -= player.vitesse end
    if keyboard.RIGHT then dx += player.vitesse end
  end

  if world1 then
    player.x += dx
    for w in walls
      if checkCollision(player, w) then
        player.x -= dx
        break
      end
    end

    player.y += dy
    for w in walls
      if checkCollision(player, w) then
        player.y -= dy
        break
      end
    end
  end
  
  idle = (dx == 0 and dy == 0)
end

drawPlayer = function()
  if idle then
    screen.drawSprite( "player/idle", player.x - camera.x, player.y - camera.y, player.lar, player.lon )
  end
end

You need to call the function in the update block

First, you need to call the functions in the appropriate functions (drawPlayer() -> draw()).

Second, world1 is not set, at least I cannot see that. When a variable is not set, it will automatically become a 0, which microscript interprets as FALSE. This means that the code to move your player will NEVER run.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community