Mario Fire Ball
In this tutorial you will learn how to make fire balls like in Super Mario Bros.
Summary
- Create a Fireball Object
- Make the Fireball fall with Gravity
- Make the Fireball Bounce on the Floor
- Make the Character Shoot the Fireball
- Make a Fire Power Object
- Give Character Abiltiy to Shoot Fireballs
- Optional: Change Character Sprite when Has Fire Abilty
Download Example
Before you Start
You will need the following:
- Platform game engine which includes a character that is pulled down by gravity and a wall.
- A sprite for a fire power pickup and a sprite for the fireball, download
mine, here and here
- Make sure the fireball sprite has precise collision checking disabled.
- Make sure the fireball sprite's bounding box is set to bull image.
- Optional, Two Types of Character Sprite , download mine, here and here
If you don't have a platform engine, follow this tutorial or download the example, Platform Movement
You need to create a spring object,
- Add a New Object
- Set the name as o_fireball
- Set the sprite as your fireball sprite
Make the Fireball fall with Gravity
To get the effect of the fireball in Super Mario Bros, it needs to bounce along the floor, for this, it will need gravity to pull it to the floor and a jump action to make it bounce back up. When it is created by the character object, the direction motion is set, the fireball object only bounces up and down.
- Open the Object Properites for the Fireball Object
- Add a Create Event
On the create event, you want to set the gravity for the fireball so it will fall back down after it bounces up when hitting the floor. Unlike the gravity on the character, we only set this once so it is always pulling the fireball down.
- Add the Action, Set Gravity from the Move Tab
- Set the Direction as, 270
- Set the Gravity as 1
- Add a New Event, Collision with Wall, choose your parent wall object to
collide.
- Add the Action, Move in Contact Direction from the Move Tab
- Set the Direction as, direction
- Set the Maximum as, 12
This Action will move your fireball out of the wall object if it gets stuck in it.
- Add the Acton, Set Vertical Speed from the Move Tab
- Set the Vert Speed as, -7
This is how high the fireball will bounce off the ground when it collides. It's recomended that you create a variable on the create event with the bounce height in it, this way, it's easier to change or make other fireballs with the original as the parent.
Make the Fireball Bounce on the Floor
The previous Actions will bounce the fireball off the ground, we now need
Actions to check if it’s hit a wall that is to the left or right.
Because the fireball is moving at a speed set in the hspeed variable, we can
simply check if there is a collision with a wall, hspeed on the x axis. If
there is, remove the fire ball.
- Open the Fireball's collision event with the Wall
- Add the Action, If there
is a Collision at Position from the Control Tab
- Set X as, hspeed
This will check the next position the fireball will move into because it is moving at the speed of hspeed. Because this is an If statement, the next Action block after it will run only if the statement is true, if there is a wall in the fireballs path.
- Add the Action, Destroy Instance from the Main 1 Tab
This is all the Actions you need for the fire ball object, basically, when the fireball hits the wall object or a child of the wall, it will bounce up in the air. At the same time, it will check if there is a wall in the direction it is moving, if there is, it will destroy itself cancelling out the bouncing.
Make the Character Shoot the Fireball
Now that you have a working fireball object, we need the character to fire them.
- Open the Object Properties of your Character
- Open the Create Event, if there isn’t one, Add a New Event, Create
On the create event you need to have two variables, one to store what direction the character should shoot the fire balls and another to say if the character has the fire ability. The second variable is optional but adds more of a challenge to your game, if you start without the fire ability and have to find a power up like in Super Mario Bros.
- Add the Action, Set Variable from the Control Tab
- Set Variable as, shoot_dir
- Set Value as, 0
If you remember the rotation in Game Maker, 0 degrees is Right, going from 0 anti clockwise back round to zero, so 90 would be Up, 180 Left and 270 Down.
- Open the Left Keyboard Event, you should have this, if not, follow the
platform tutorial.
- Add the Action, Set Variable from the Control Tab
- Set Variable as, shoot_dir
- Set Value as, 180
- Do the same for the Right Keyboard Event but set the value of the variable as 0.
This set of action sets the direction the character is moving in degrees. This variable is used to determine which direction to shoot the fireball, it will not shoot it, just set what direction it should move when it is created.
- Add a New Event, Key Press / Space Bar, if you don’t want Space Bar
to fire, change the Key but use the same type of event.
- Add the Action, If Variable has a Value from the Control Tab
- Set Variable as, has_fire
- Set Value as, true
This will check if the has_fire variable is true before allowing the character to shoot a fireball.
- Add the Action, Create Instance with Motion from the Main 1 Tab
- Set the Object as, o_fireball (or whatever your fireball object is)
- Set the Speed as, 12
- Set the Direction as, shoot_dir
- Tick Relative
This will now create a fireball and shoot it in the direction of the shoot_dir value, this value is set to either 0 or 180 when the player presses left or right making the fireball shoot in the characters facing direction. The player will only be able to shoot fireballs if the has_fire value is true, by default it is set as false, in the next step we will create an object that sets has_fire to true.
Optional:
At the moment, when a fireball is created, it’s fired downwards because on it’s create event, gravity is set and automatically brings it down to the ground. In this create, after setting the gravity, If you set the vspeed to a negative number, say, -3, the fireball will be created but look as if it was thrown upwards. Have a mess around with that and see what results you can get.
In the game Super Mario Bros, you can only use the fireballs when you have the fire ability, this is set when the character collects a power up in the shape of a flower which enables the ability to shoot fireballs.
You need to create a Fire Pickup Object, collecting this will enable the character to shoot fireballs.
- Add a New Object
- Set the name as o_fireball_pickup
- Set the sprite as your fireball pickup sprite
Give Character Abiltiy to Shoot Fireballs
Now you have a fireball pick up object, you can create a collision event with it that changes the has_fire variable on the character to true, doing this, will allow the character to shoot a fireball when they press space.
- On the Chracter Object, Add a New Event, Collision with the Fire Power
Up Object
- Add the Action, Set Variable
- Set Variable as, has_fire
- Set Value as, true
- Add the Action, Destroy Isntance
- Select the Option, Other
This code will run when the character collides with the power up, it will set the has_fire value to true, which will allow the character to shoot fireballs and also remove the Fire Power Up object.
Optional: Change Character Sprite when Has Fire Abilty
In Super Mario Bros, the character's sprite changes when it has the Fire Power.
You can do this by using the, If Variable has a Value Action and an Else.
Example:
IF has_fire EQUAL TO true
Change Sprite to s_char_fire
ELSE
Change Sprite to s_char_normal
See if you can figure this one out, it is good practice.