Landers | Writing the script to destroy the invader and play the explosion.

We need two scripts to destroy the invader. The first will be the invader responding to the collision with the missile. Since it is controlled by the swarm object it will need to send a message to the swarm to say it has been hit. The second script for the swarm object will respond to receiving the message and update its swarm table.

  1. In the assets panel, right click the 'main' folder, and select, 'New...', 'Script'.
  2. Name the script, 'invader'.
  3. Change the on_message function to respond to being hit.
function on_message(self, message_id, message, sender)
	if message_id == hash("collision_response") then
		msg.post("/swarm", "hit", { object_id = go.get_id() })
	end
end
What does this code do?

If the invader receives a message from the collision handler it posts a message to the swarm game object to say it has been "hit". We could have used any string value here providing we are consistent.

We can retrieve the Id of the current invader using go.get_id(). This is passed as an argument with the message.

Now we need to attach the script and the particle effect we created to the invader.

  1. In the assets panel, in the 'main' folder, double click, 'invader.go'.
  2. In the outline panel, right click, 'Game Object' and select, 'Add Component File'.
  3. Select '/main/invader.script'.
  4. In the outline panel, right click, 'Game Object' and select, 'Add Component File'.
  5. Select '/main/explosion.particlefx'.
  6. In the assets panel, in the 'main' folder, double click, 'swarm.script'.
  7. Change the on_message function to respond to an invader in the swarm being hit.
function on_message(self, message_id, message, sender)
	if message_id == hash("hit") then
		local alien_hit = message.object_id
		for key,invader in ipairs(self.swarm_table) do
			if alien_hit == invader then
				particlefx.play(invader)
				table.remove(self.swarm_table,key)
				go.delete(alien_hit)
				self.speed = self.speed + 10
			end
		end
	end
end
What does this code do?

If the message received is "hit" then an invader has been hit and we need to remove the correct one.

alien_hit is a local variable that holds the Id of the object that sent the message. It might seem odd that we aren't just using the sender parameter here. Unfortunately that isn't quite the Id we stored in the table:

  • Id in the table: /instancex
  • Id of the sender: main:/instancex#invader

We could use some string manipulation to extract the instance but that is a lot of effort when we can pass the Id as a parameter in the message.

We iterate over the invaders in the swarm_table. If the invader is the one that was hit the particle effect is played. The invader Id is removed from the table and the game object deleted.

Because the explosion.particleFX file is a component of the invader game object, it is played at the location the invader was on the screen.

Finally, we increase the speed of the invaders by 10. A lower number makes the game easier. A higher number makes the game harder. If we were thinking ahead we might use a variable instead of hard-coding a constant here.

  1. Save the changes by pressing CTRL-S or 'File', 'Save All'.
  2. Run the program by pressing F5 or choosing, 'Debug', 'Start / Attach' from the menu bar. The invaders should disappear in an explosion when they are shot.
    Run

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