controller.script 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. -- speed of the time in the collection proxy
  2. go.property("speed", 1)
  3. function init(self)
  4. -- acquire input for this script
  5. msg.post(".", "acquire_input_focus")
  6. -- load the collection proxy
  7. msg.post("#gameproxy", "async_load")
  8. end
  9. function update(self, dt)
  10. -- update the time step of the proxy each frame since it might be animated
  11. msg.post("#gameproxy", "set_time_step", { factor = self.speed, mode = 0 })
  12. label.set_text("#label", tostring(self.speed))
  13. end
  14. function on_message(self, message_id, message, sender)
  15. if message_id == hash("proxy_loaded") then
  16. msg.post(sender, "enable")
  17. elseif message_id == hash("animate_speed") then
  18. -- cancel any current animation of the speed property
  19. go.cancel_animations("#", "speed")
  20. -- start animation of the speed property
  21. local to = message.to
  22. local change = math.abs(self.speed - to)
  23. local rate_of_change = 2
  24. local duration = change / rate_of_change
  25. go.animate("#", "speed", go.PLAYBACK_ONCE_FORWARD, to, go.EASING_LINEAR, duration)
  26. elseif message_id == hash("change_speed") then
  27. -- cancel any current animation of the speed property
  28. go.cancel_animations("#", "speed")
  29. -- make sure speed never goes below 0
  30. self.speed = math.max(self.speed + message.amount, 0)
  31. end
  32. end