Adding physics

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:

Adding physics

Applying force at an angle

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:

Applying force at an angle

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.

Applying force at an angle

Setting cannonballs to fire one at a time

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:

Setting cannonballs to fire one at a time

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:

Setting cannonballs to fire one at a time

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:

Setting cannonballs to fire one at a time

Setting the viewpoint to follow the cannonball

You will notice that the viewpoint or camera does not follow the cannonball. Double-click on the cannonball and add a ScrollTo behavior, as shown in the following screenshot:

Setting the viewpoint to follow the cannonball

Also, add a Destroy outside layout behavior, as shown in the following screenshot:

Setting the viewpoint to follow the cannonball

Making an immovable ground

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:

Making an immovable ground

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.

Making an immovable ground

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:

Making an immovable ground

Reloading the cannon

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:

Reloading the cannon

Create the On destroyed event and set canFire to 0 by adding in a System set value of a variable.

Reloading the cannon

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:

Reloading the cannon

The object for this event is going to be the cannon, as shown in the following screenshot:

Reloading the cannon

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:

Reloading the cannon
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.133.160