parent.script 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. function init(self)
  2. msg.post(".", "acquire_input_focus") -- <1>
  3. local pos = go.get_position() -- <2>
  4. go.animate(".", "position.y", go.PLAYBACK_LOOP_PINGPONG, pos.y + 300, go.EASING_INOUTSINE, 3) -- <3>
  5. self.has_child = true -- <4>
  6. end
  7. function on_input(self, action_id, action)
  8. if action_id == hash("touch") and action.pressed then
  9. if self.has_child then
  10. msg.post("child", "set_parent", { keep_world_transform = 1 }) -- <5>
  11. label.set_text("#label", "Click to child...") -- <6>
  12. else
  13. msg.post("child", "set_parent", { parent_id = go.get_id(), keep_world_transform = 1 }) -- <7>
  14. label.set_text("#label", "Click to detach...") -- <8>
  15. end
  16. self.has_child = not self.has_child -- <9>
  17. end
  18. end
  19. --[[
  20. 1. Tell the engine that this game object wants to receive input.
  21. 2. Get the current position.
  22. 3. Animate the y position of this game object back and forth.
  23. 4. A flag to track if there is a child to this game object or not. Parent-child relations in Defold are only affecting the scene graph so we need to track this ourselves.
  24. 5. If the user clicks and there is a child, set the parent to nothing, meaning remove it as a child.
  25. 6. Set the label text accordingly.
  26. 7. If the user clicks and there is no child, set the parent to this game object.
  27. 8. Set the label text accordingly.
  28. 9. Set the flag to its inverse.
  29. --]]