如何在lua中按下右箭头时以动画方式显示精灵?

我是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)
    --添加最终化代码
    --如果不需要,删除此函数
end

function update(self, dt)
end

function on_message(self, message_id, message, sender)
    --在此处添加消息处理代码
    --如果不需要,删除此函数
    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

这是教程中的代码。就像我所说的,当我按下右箭头时,它不动。

点赞
用户5023201
用户5023201

你在函数on_input的第一个if语句中拼错了单词'action'。

这个脚本应该可以运行:

local currentAnimation = 0

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

function final(self)
    -- 在这里添加最终化代码
    -- 如果不需要可以移除此函数
end

function update(self, dt)

end

function on_message(self, message_id, message, sender)
    -- 在这里添加消息处理代码
    -- 如果不需要可以移除此函数
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
2018-08-13 12:23:28