2
0

spaceship2.script 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. function init(self)
  2. msg.post(".", "acquire_input_focus") -- <1>
  3. self.moving = false -- <2>
  4. end
  5. local function landed(self) -- <6>
  6. self.moving = false
  7. label.set_text("#speech", "Hey, go to the opposite side!")
  8. local pos = go.get_position()
  9. local opposite = vmath.vector3()
  10. opposite.x = 720 - pos.x
  11. opposite.y = 720 - pos.y
  12. msg.post("spaceship1#script", "go to", { position = opposite })
  13. end
  14. function on_message(self, message_id, message, sender)
  15. if message_id == hash("go to") then -- <5>
  16. self.moving = true
  17. label.set_text("#speech", "I'm going...")
  18. go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, message.position, go.EASING_INOUTCUBIC, 1.5, 0, landed)
  19. elseif message_id == hash("i'm there") then -- <7>
  20. label.set_text("#speech", "Great!")
  21. end
  22. end
  23. function on_input(self, action_id, action)
  24. if action_id == hash("touch") and action.pressed and not self.moving then -- <3>
  25. local pos = vmath.vector3(action.x, action.y, 0)
  26. msg.post("#", "go to", { position = pos }) -- <4>
  27. end
  28. end
  29. --[[
  30. 1. Tell the engine that we want to receive input.
  31. 2. Store a flag in the current script component instance that tells us if the spaceship is moving or not.
  32. 3. If user clicked and the spaceship is not moving.
  33. 4. Send a message to this script component ("#" is shorthand for that) saying "go to" and the clicked position
  34. as part of the message data.
  35. 5. If a "go to" message is received, set the speech label text and then animate the position of the current
  36. game object ("." is shorthand for that) to the position send in the message data. When the animation is
  37. done the function `landed()` is called.
  38. 6. When `landed()` is called on animation complete, set the label text, then calculate a position on the
  39. opposite of the screen and send a message called "go to" to the component "script" in the game object
  40. "spaceship11". Supplied with the message is the opposite position as message data.
  41. 7. If someone sends us a message called "i'm there" we react by just changing the speech label text.
  42. --]]