当我按下向右箭头时,如何在lua中设置精灵动画?

时间:2018-08-12 21:50:32

标签: lua 2d sprite defold

我是Defold和编码的新手,我一直在关注从Gamefromscratch到动画精灵的视频教程,这是一个https://www.youtube.com/watch?v=ha1Wq2FB7L0&t=5s,但是当我按向右箭头时它无法移动,它只是站立处于空闲位置。

local currentAnimation = 0

function init(self)
    msg.post(".", "acquire_input_focus")
end

function final(self)
    -- Add finalization code here
    -- Remove this function if not needed
end

function update(self, dt)
end

function on_message(self, message_id, message, sender)
    -- Add message-handling code here
    -- Remove this function if not needed
    end

function on_input(self, action_id, action)
if aciton_id == hash("right") and action.pressed == true then
    if self.currentAnimation == 1 then
        msg.post("#sprite", "play_animation", {id = hash("runRight")})
        self.currentAnimation = 0
    else 
        msg.post("#sprite", "play_animation", {id = hash("idle")})
        self.currentAnimation = 1
    end
end
end

这是代码,正如我在按向右箭头时所说的那样,它不会像教程中那样移动。

1 个答案:

答案 0 :(得分:4)

您在函数on_input的第一个if语句上拼错了“ action”一词。

此脚本应该起作用:

local currentAnimation = 0

function init(self)
    msg.post(".", "acquire_input_focus")
end

function final(self)
    -- Add finalization code here
    -- Remove this function if not needed
end

function update(self, dt)

end

function on_message(self, message_id, message, sender)
    -- Add message-handling code here
    -- Remove this function if not needed
end

function on_input(self, action_id, action)
  if action_id == hash("right") and action.pressed == true then
    if self.currentAnimation == 1 then
      msg.post("#sprite", "play_animation", {id = hash("runRight")})
      self.currentAnimation = 0
    else 
      msg.post("#sprite", "play_animation", {id = hash("idle")})
      self.currentAnimation = 1
    end

    return true
  end
end