Incoming | Writing the collision message handlers.

  1. Open the 'comet.script' file.
  2. Step-by-step guide
    • In the assets panel, in the 'main' folder, double click 'comet.script'.
  3. Change the on_message function to respond to the target collision object being hit by either the earth or player collision objects.
function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response") and message.other_id == hash("/earth") then
		go.delete()
	end
	if message_id == hash("contact_point_response") and message.other_id == hash("/target") then
		go.delete()
	end
end
What does this code do?

If a message is received from the collision handler and the Id of the object sending the message is 'earth' then delete the game object (the comet).

The action is also carried out for the 'player' object.

This could be achieved in a single condition. However, thinking ahead we might want other actions to happen depending on what caused the collision. For example, if the comet had hit the planet we would want to penalise the player in some way. Perhaps by losing a life. If the comet is hit by the player target perhaps we want to increase the score. Since both conditions need to be checked anyway there is no overhead in using two IF statements here.

Note you should delete game objects whenever you can to ensure you don't max-out the number available in the object pool. Another approach would be to reset the position of the comet so that it is immediately respawned when it is destroyed.

  1. Save the changes by pressing CTRL-S or 'File', 'Save All'.
  2. Run your game at this point. It should now despawn the comets.
  3. Run

That's the core mechanics done but the game can be improved in many ways from this starting point. Stage 7 >