mouse_and_touch.script 991 B

12345678910111213141516171819202122232425
  1. function init(self)
  2. msg.post(".", "acquire_input_focus") -- <1>
  3. self.state = "-"
  4. end
  5. function on_input(self, action_id, action)
  6. local pos = vmath.vector3(action.x, action.y, 0) -- <2>
  7. if action_id == hash("touch") then -- <3>
  8. if action.pressed then -- <4>
  9. self.state = "pressed"
  10. elseif action.released then -- <5>
  11. self.state = "-"
  12. end
  13. end
  14. local text = ("x: %d y: %d state: %s"):format(pos.x, pos.y, self.state)
  15. label.set_text("#label", text)
  16. end
  17. --[[
  18. 1. Tell the engine that this object ("." is shorthand for the current game object) wants to receive input. The function `on_input()` will be called whenever input is received.
  19. 2. Read the position of the mouse pointer or touch event
  20. 3. The left mouse button in the input bindings will also be used for touch events on a phone/tablet
  21. 4. The 'pressed' state will be true on the frame when the mouse button/finger is pressed
  22. 5. The 'released' state will be true on the frame when the mouse button/finger is released
  23. --]]