follow.script 1.2 KB

1234567891011121314151617181920212223242526
  1. go.property("speed", 350) -- <1>
  2. function init(self)
  3. msg.post(".", "acquire_input_focus") -- <2>
  4. end
  5. function on_input(self, action_id, action)
  6. if action_id == hash("touch") or not action_id then -- <3>
  7. local current_pos = go.get_position() -- <4>
  8. local target_pos = vmath.vector3(action.x, action.y, 0) -- <5>
  9. local distance = vmath.length(target_pos - current_pos) -- <6>
  10. local duration = distance / self.speed -- <7>
  11. go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, target_pos, go.EASING_LINEAR, duration, 0) -- <8>
  12. end
  13. end
  14. --[[
  15. 1. The speed of the game object in pixels/second
  16. 2. Tell the engine that this game object ("." is shorthand for the current game object) should listen to input. Any input will be received in the `on_input()` function.
  17. 3. Check if we received mouse movement (no action id) or an input action named "touch" (touch or mouse click)
  18. 4. Get the current position of the game object.
  19. 5. Set the target position to the position of the mouse or touch.
  20. 6. Calculate the distance (length) between the current and target position.
  21. 7. Calculate the time it takes to travel the distance given the speed of the game object.
  22. 8. Animate the game object's ("." is shorthand for the current game object) position to `target_pos`.
  23. --]]