Top Down Movement and Animation in Defold - Defold 2D Tutorial 5
Автор: RedDotClipsGames
Загружено: 2025-04-08
Просмотров: 144
                Описание:
                    A tutorial on top down movement and animation in Defold.
Script:
function init(self)
 msg.post("#", "acquire_input_focus")
 msg.post("Gui", "disable")
 self.in_dir = vmath.vector3(0,0,0)
 self.speed = 100
 self.actions = {} -- map actions to self
 self.current_animation = nil
end
local function animate(self,animation)
 if self.current_animation ~= animation then
  self.current_animation = animation
  sprite.play_flipbook("#Player Sprite", animation)
 end
end
function update(self, dt)
 --horizontal and vertical animation handlers
 if self.actions[hash("Left")] then
  animate(self, "Walk Left")
  self.in_dir.x = -self.speed
 elseif self.actions[hash("Right")] then
  animate(self, "Walk Right")
  self.in_dir.x = self.speed
 else
  self.in_dir.x = 0
 end 
 if self.actions[hash("Down")] and not self.actions[hash("Left")] and not self.actions[hash("Right")] then
  animate(self, "Walk Down")
  self.in_dir.y = -self.speed
 elseif self.actions[hash("Up")] and not self.actions[hash("Left")] and not self.actions[hash("Right")] then
  animate(self, "Walk Up")
  self.in_dir.y = self.speed
 else
  self.in_dir.y = 0
 end
 
 --2 button conflict animation handlers
 if self.actions[hash("Left")] and self.actions[hash("Right")] then
  self.in_dir.x = 0
 elseif self.actions[hash("Up")] and self.actions[hash("Down")] then
  self.in_dir.y = 0
 end
 --idle script
 if self.in_dir.x == 0 and self.in_dir.y == 0 then
  animate(self, "Idle")
 end
 --move script
 go.set_position(go.get_position() + self.in_dir * dt)
 
 
end 
function on_message(self, message_id, message, sender)
 pprint(message_id,message)
end
function on_input(self, action_id, action)
 --allows getting actions in update function
 if action_id then
  if action.pressed then
   self.actions[action_id] = true
  elseif action.released then
   self.actions[action_id] = false
  end
 end                
                
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
- 
                                
Информация по загрузке: