player.script 957 B

1234567891011121314151617181920212223242526
  1. function init(self)
  2. -- make sure the script will receive user input
  3. msg.post(".", "acquire_input_focus")
  4. end
  5. function on_input(self, action_id, action)
  6. -- mouse or spacebar
  7. if (action_id == hash("touch") or action_id == hash("key_space")) and action.pressed then
  8. -- position bullet somewhat offset from the player position
  9. local pos = go.get_position()
  10. pos.y = pos.y + 50
  11. -- spawn a bullet
  12. local bullet_id = factory.create("#bulletfactory", pos)
  13. -- animate the bullet
  14. local distance = 1000 -- distance in pixels
  15. local speed = 800 -- pixels per second
  16. local duration = distance / speed -- time in second to travel the full distance
  17. local to = pos.y + distance
  18. -- start animation and delete bullet when it has reached its destination
  19. go.animate(bullet_id, "position.y", go.PLAYBACK_ONCE_FORWARD, to, go.EASING_LINEAR, duration, 0, function()
  20. go.delete(bullet_id)
  21. end)
  22. end
  23. end