Game Development Reference
In-Depth Information
Fighting back
So we can now place cannons that shoot cannonballs in the direcion of the mouse cursor.
But we sill aren't hiing any enemies. Let's quickly ix that problem.
Engage thrusters
We will check when a cannonball and an enemy collide and take proper acion accordingly.
The steps to do it are as follows:
1. Inside the cannonball, clone repeat , and place an if … then condiion block to
check collisions.
2. Check for collisions by using the touching <enemy> block.
3. When a cannonball touches an enemy, first wait for 0.05 seconds by using the
wait ... secs block. This is to ensure that the enemy has ime to respond to the
collision as well.
4.
Then we add a delete this clone block to delete the clone.
So cannonballs will not only expire after having traveled a certain distance, but also when
they hit an enemy. You could omit this step, but that would make the game a bit too easy
because cannonballs can move on and hit muliple enemies, as was the case in the Arillery
Game before we fixed that.
The enemies have to respond to being hit by a cannonball as well. So let's add that
condiion to their script. The condiion to check for the enemy is actually the same as
for the cannonball, but in reverse order. So the easiest way of adding it is to copy the
script segment and change the variable pieces as shown in the following steps:
1.
Click-and-drag the if statement from the cannonball to the enemy sprite in the
Sprites view.
2.
Click on the enemy sprite in the Sprites view to look at the enemy scripts.
You will see the segment you dragged has been copied to the enemy scripts.
 
Search WWH ::




Custom Search