dynamic.script 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. function init(self)
  2. msg.post(".", "acquire_input_focus")
  3. -- a list of different bullet prototypes
  4. self.bullets = {
  5. "/example/flame.goc",
  6. "/example/lightning.goc",
  7. "/example/rock.goc",
  8. }
  9. -- the currently used bullet prototype
  10. self.bullet_index = 1
  11. -- shoot one bullet per second
  12. -- animate the bullet up 1000 pixels and then delete it
  13. timer.delay(0.2, true, function()
  14. local id = factory.create("#bulletfactory")
  15. local to = go.get_position(id)
  16. to.y = to.y + 1000
  17. go.animate(id, "position", go.PLAYBACK_ONCE_FORWARD, to, go.EASING_LINEAR, 1.5, 0, function()
  18. go.delete(id)
  19. end)
  20. end)
  21. end
  22. function on_input(self, action_id, action)
  23. -- mouse or spacebar
  24. if (action_id == hash("touch") or action_id == hash("key_space")) and action.pressed then
  25. -- next bullet prototype, wrap around to the first
  26. self.bullet_index = self.bullet_index + 1
  27. if self.bullet_index > #self.bullets then
  28. self.bullet_index = 1
  29. end
  30. -- unload current prototype
  31. factory.unload("#bulletfactory")
  32. -- set a new prototype
  33. local prototype = self.bullets[self.bullet_index]
  34. factory.set_prototype("#bulletfactory", prototype)
  35. end
  36. end