collisions.script 1020 B

12345678910111213141516171819202122232425262728293031
  1. function init(self)
  2. msg.post(".", "acquire_input_focus") -- <1>
  3. for i=1,10 do
  4. factory.create("#enemyfactory", vmath.vector3(math.random(100, 700), 600, 1)) -- <2>
  5. end
  6. end
  7. function on_input(self, action_id, action)
  8. if action_id == hash("mouse_button_left") and action.pressed then
  9. factory.create("#enemyfactory", vmath.vector3(action.x, action.y, 1)) -- <3>
  10. end
  11. end
  12. function on_message(self, message_id, message, sender)
  13. if message_id == hash("collision_response") then -- <4>
  14. if message.own_group == hash("danger") then -- <5>
  15. go.delete(message.other_id) -- <6>
  16. end
  17. end
  18. end
  19. --[[
  20. 1. Acquire input for the script
  21. 2. Spawn 10 game objects at random positions near the top of the screen
  22. 3. Spawn a game object when the left mouse button (or touch) is pressed
  23. 4. Something collided with the tilemap if the received message was a `collision_response`
  24. 5. Check if something collided with a tile belonging to the collision group "danger"
  25. 6. Delete the game object that collided with the tilemap
  26. --]]