0

我是 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

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

4

1 回答 1

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
于 2018-08-13T12:23:28.323 回答