Discord
Login
Community
DARK THEME

Animations

Is there a way to add movement based animations with or without conditions? For example:

if keyboard.RIGHT then
screen.drawSprite("player_right",player.x,player.y,20)
else
screen.drawSprite("player",player.x,player.y,20)
end

Yes, here's a code example. You need to create a variable animation = "" in your init file and define it with an example condition.


init = function()
  player = object
  x = 0
  y = 0
  anim = "idle" // Default animation state
end

update = function()
  local moving = false // Track if any key is pressed
  
  if keyboard.UP then
    player.y += 2
    player.anim = "up"
    moving = true
  elseif keyboard.DOWN then
    player.y -= 2
    player.anim = "down"
    moving = true
  elseif keyboard.LEFT then
    player.x -= 2
    player.anim = "left"
    moving = true
  elseif keyboard.RIGHT then
    player.x += 2
    player.anim = "right"
    moving = true
  end
  
  // If no movement keys are pressed, set state to idle
  if not moving then
    player.anim = "idle"
  end
end

draw = function()
  screen.clear()
  // Use ".." to combine strings  
  // Ensure your sprites are named: player_idle, player_up, etc.
  screen.drawSprite("player_" .. player.anim, player.x, player.y, 50, 50)
end

Post a reply

Progress

Status

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