Run the game again and you will see that the cannonball does not act like a cannonball. We need to add some physics to it! Click on the cannonball and add the Physics behavior as shown in the following screenshot:
Run the game again.
You will notice that the ball just falls down! Now that we have added physics, we can add a force to the cannonball to make it shoot like real cannon. Add an action to the Mouse click event. The action will be Apply force at angle, as shown in the following screenshot:
Set the force to around 200
. The bigger the number, the further the cannonball travels. For the angle, type in cannon.Angle
as shown in the following screenshot. This will ensure that the cannonball is shot in the same direction as the cannon.
If you run the game, you will notice that we can fire as many cannonballs as we want. We should limit the cannonballs to make them fire only one at a time. In order to do this, we need to add a global variable. Let's name this variable canFire
and set its initial value to 0
, as shown in the following screenshot:
Click on the On any click mouse event and press C to add another condition. Navigate to System | Compare variable. Set the canFire
variable to be equal to 0
(using the Equal to condition). This is shown in the following screenshot:
Once you've done that, add a System action to the On any click mouse event that sets canFire
to be equal to 1. This will make the cannon fire only one at a time. If you run the game now, you will only be able to fire one cannonball. We will add the "reloading" functionality in a moment. The event sheet should look like the following screenshot:
When running the game, you will see that the camera now does indeed follow the cannonball. However, there is only one problem—there is no "ground" and the cannonball just falls to the bottom of the screen. So, let's make a "ground"! Add another sprite and resize it to 1500
by 250
. Put this on a background layer. Let's give the background a blue color—something that stands out. Check the following screenshot:
Add a Physics behavior to the ground sprite. Set Immovable to Yes and Prevent rotation to Yes, as shown in the following screenshot. This will make this sprite immoveable.
Once you have done this, place the sprite near the bottom of the screen and set Opacity to 0
so that it's transparent. This is shown in the following screenshot:
We need to set canFire
back to 0
so that the cannon can reload and fire another cannonball. Add an On destroyed event to our cannonBall
sprite, as shown in the following screenshot:
Create the On destroyed event and set canFire
to 0
by adding in a System set value of a variable.
After that, we need to scroll back to the cannon so that the next cannonball will be fired from there. To do this, we need to set in a Scroll to object event for the System, as shown in the following screenshot:
The object for this event is going to be the cannon, as shown in the following screenshot:
Your event should look like the following screenshot. Run the game. You will notice that when the cannonball goes off of the screen, it gets destroyed. When this happens, the camera scrolls back to the cannon and you can fire again! The event sheet now looks like the following screenshot:
18.119.133.160