Make a Cannon in Gamefroot

Note: this tutorial is out of date. You can use the concepts from this tutorial with this updated tutorial http://gamefroot.com/knowledgebase/pow-shooting-projectiles-missiles-bullets-lasers/

Adding a Script

This tutorial relies heavily on the use of advanced script code blocks. To use advanced script code blocks you must edit an items behaviors. The way that is done is by first right clicking an item on the stage and selecting “edit unique simple behavior on this item…”

AddingScript

When you have done this you will get taken to a page like this. Click the advanced tab at the top of this window.

AddingScriptBeforeAdvanced2

 

You will then have to hit the add advanced behavior button in the center of the window.

AddingScriptCAdvanced2

 

Custom Images

You can also use these custom images or some of your own if you follow this tutorial by clicking here!

Cannon  WheelCannonBall

Just right click the image and click “Save image as…”

 

Try the game!

 

PlayScreen

 

Making the Cannon

It is quite simple to make a cannon and cannon ball in Gamefroot. There are two parts that need to be made. The first part is the cannon ball. A cannon ball can go different speeds and go in all direction. This means that we will have to work with its X and Y velocity. The way we are working out its velocity is the distance between the shooting point and the mouse position. In this first part the shooting point will be the cannon ball itself. This can be easily calculated by using these code blocks.

Cannon_Ball_1

With this code block it figures out the position of the mouse and subtracts the location of the ball for each of the X and Y vales. This gives small enough values to set the X and Y velocity of the cannon ball. The end result should look like this:

Stage 1Edited

 

After this we want to make a cannon that will shoot the ball. The main feature of the cannon will be that it will always point in the direction of the mouse. Therefore you can tell where the cannon ball will shoot. This can be done using the “Rotate Sprite” block found in the animation tab. Seeing as we want the cannon to face the mouse we have to work out the angle between the mouse and the cannon.   This will be done by using these code blocks:

Gun_Part_0

(Note: The + 90 degrees is because the original angle of the cannon is 90 degrees off) To make the cannon ball shoot from the cannon we have to make the cannon ball teleport to the cannon when the mouse is clicked. So the cannon script will look like this.

Gun_Part_1

 

(Note: 1774_5ce51e5 is the instance of the cannon ball on my stage)

After this your level should start looking like a game!

Stage 1_5

 

Cannon balls don’t travel in straight lines. They are normally affected by gravity. To make the cannon balls motions more realistic add these code blocks. These code blocks work as gravity and wind resistance. They can be changed if they are too strong or too weak.

Cannon_Ball_2

 

 

After these your game should look more realistic like this!

Stage 2Edited

Multiple Cannon Balls

The ability to have more than one cannon ball shooting at once is a little harder to do but I will go into it here. The first thing you need to understand is to have multiple copies of one thing on the map at once means that you have to have the original on the map to begin with. In my example I’ve placed the original cannon ball at the bottom left corner of the level with the co-ordinates of (0 x, 2832 y). This is important because you do not want to original ball to be able to shoot. To make sure the original does not shoot you have to make an ‘When First Created’ block that detects if the balls position is at (0, 2832), if the balls position is not at that position(aka it is at the shooting position) you want the cannon ball to shoot as it has done previously. To make the spawned cannon ball different to the original you will change its health from 100 to 99 by subtracting 1 from its original health. After this you want it to shoot. To do this you will send a message to the sprite itself saying “Shoot!”. When the sprite hears the message “Shoot!” it will do the original shoot code block and also set the health to 98 by subtracting another 1. Doing this ensures that the ball will never shoot again.   Your cannon ball script should now look like this.

Cannon_Ball_3

 

(Note: I have also doubled the velocity by multiplying it by 2 and added a Die method which will kill the sprite after 4000 ms (4 seconds))     Doing this means that the original sprite never moves. Therefore we have to create copies of sprites that move in its place. These sprites spawn on the cannon, so we would use this block of code instead of the “Teleport To” block.

Gun_Part_2_A

 

 

Try the game!

 

PlayScreen

Leave a Reply