Chapter 7
In This Chapter
Creating buttons to trigger particles
Using the Create Effect Action
Creating several types of fireworks effects
Coding simple and complex particles
Coding emitters
In GameMaker, you can make some very special effects, such as explosions and fireworks. You create these effects through the use of particles. Particles are fast graphics resources that are generated based on random values. You can create special effects for your game by
In this chapter, I walk you through these various methods by using the Particles tutorial that is included with GameMaker. For the coding aspect, you can go ahead and skip to the next chapter, because this chapter primarily discusses using the drag-and-drop Actions. However, I do show you how to create a button to trigger these effects in this chapter, which you can also use in Chapter 8.
As with the other chapters in this book, you can find the assets for this tutorial by choosing Help⇒Open Project in Explorer⇒Assets, from the main menu. Also, when you’re done with this tutorial, you can install the demo for using particles to compare your work.
Figure 7-1 shows an example of a special effect that you can make by following along with this chapter.
To get started with particles, I show you how to create a fireworks display using the Create Effect Action. First, I describe how to create a button to trigger the effect.
Before you get started with creating particles, I provide a procedure from the in-software Particles tutorial on how to create a button that the player can use to control the particles. Basically, when the player presses the button, a fireworks effect will appear. You duplicate this button and use it to trigger the other methods discussed in this chapter and Chapter 8, on how to create special effects.
To create a button for triggering the particles, follow these steps:
The Object Properties window appears.
A thumbnail of the image appears in the Sprite section and the field is populated with the name of the image.
The Create Event appears in the Events section.
A code window appears.
image_speed = 0;
image_index = 0;
This code stops the button from animating (if you opened the Sprite, you may have noticed that the assigned Sprite is made up of three different sub-images) and tells GameMaker to display only the first indexed sub-image.
GameMaker starts counting at 0, just as in programming. Each sub-image represents a different state of the button — image 0 is the unselected state of the button, image 1 shows the button when the player hovers over it with the mouse, and image 2 is a slightly smaller version of the image to represent when the button is actually clicked on by the player. The final result is a button that reacts when the player either hovers over it or clicks it. (You write the code for that in Step 14.)
The code window is saved and closed.
The Alarm 0 Event appears in the Events section.
The code window appears.
image_index = 0;
You just set an Alarm that when triggered, displays the first sub-image.
The code window is saved and closed.
The Step Event appears in the Events section.
The code window appears.
if instance_position(mouse_x, mouse_y, id)
{
if image_index != 2
{
image_index = 1;
if mouse_check_button_pressed(mb_left)
{
alarm[0] = room_speed / 2;
image_index = 2;
instance_create(random(room_width), 400, obj_button_firework);
}
}
}
else
{
if image_index != 2
{
image_index = 0;
}
}
This code determines which sub-image is displayed, depending on the state of the mouse (is the player hovering, is the player not hovering, or is the player clicking the button). The code also tells GameMaker to check if the mouse position is over the button, to check if the mouse button has been pressed, and if so, to create the firework Object. The player sees a hover and pressed state for the button. At the end of the code, it resets the image index back to 0 (the player is not hovering or clicking).
The preceding code creates an Instance of the firework Object, but because you haven’t created this Object yet, the code window displays the Object text (obj_button_firework) in white. After you’ve created the object, the Object text in the code window appears in red, as shown in Figure 7-2.
The code window is saved and closed.
The Object Properties window is saved and closed.
In the preceding procedure, you created a button that will trigger a fireworks effect. In this section, I show you how to create the effect itself by using the Create Effect Action, which does require some coding by hand. In the following procedure, you create the firework Object.
To create the firework Object, follow these steps:
The Object Properties window appears.
The Create Event appears in the Events section.
A code window appears.
direction = point_direction(x, y, room_width / 2, 100);
speed = 15 + random(5);
gravity = 0.5;
gravity_direction = 270;
alarm[0] = 1;
alarm[1] = room_speed
The code window is saved and closed.
The first line of this code sets the direction of the firework so that it moves toward the center of the Room, it has speed, and there’s also gravity so that it appears as if the firework’s sparkles start to fall naturally. This code also has two Alarms — one to create a smoke trail and the other to generate the actual firework. You configure the Alarms next.
The Alarm 0 Event appears in the Events section.
The Comment window appears.
This is a note to yourself for easy reference later on what the Alarm entails.
The Comment window is saved and closed. In the Actions section, you can now see the Comment you just wrote, as shown in Figure 7-3.
Now you need to add the Create Effect Action.
The Create Effect properties window appears.
Smoke appears in the Type field (see Figure 7-4).
Small appears in the Size field.
The Color window appears.
The color gray appears in the Color field.
Below Objects means that this Object will be drawn beneath other Objects at a depth of 100,000. If you choose above, then this Object would appear over other Objects at a depth of –100,000.
Now the properties are relative to what is happening in the game.
The Create Effect properties is saved and closed.
Now you need to set an Alarm Action.
You’re going to use this Set Alarm Action to reset Alarm 0 every step of the game. When Alarm 0 is triggered (you set this up in the last procedure), a smoke particle is created, which creates a trail as the Instance moves across the Room.
The Set Alarm Action window appears.
This value determines how often the Alarm is triggered. In this case, the Alarm will be triggered once for every step of the game.
This is the Alarm that will be triggered every step of the game.
The Set Alarm Action window is saved and closed.
Now add the second Alarm Event.
The Alarm 1 Event appears in the Events section.
The Comment Action window appears.
The Comment Action window is saved and closed. You can now see the Comment Action in the Actions section.
A Create Effect Action window appears.
The firework will appear relative to the Instance with the Action.
The Color window appears.
The Color window is saved and closed. The color you picked appears in the Color field.
This particle will now be drawn underneath other Instances.
The Create Effect Action window is saved and closed.
Now add a Set Alarm Action to the Alarm[1] Event.
The Set Alarm Action window appears.
The Set Alarm Action window is saved and closed.
Now to add a second Set Alarm Action for the Alarm[1] Event.
A Set Alarm Action window appears.
You just set this Alarm to trigger three times for every step of the game.
Your Alarm 1 Event should look like Figure 7-6.
Next, you need to create an Event for Alarm[2] that destroys the Instance of the firework effect. It’s important to destroy the Instance — you don’t want anything in the Room that doesn’t do anything because it will eventually build up and slow down the game.
To destroy the firework effect, follow these steps:
The Alarm 2 Event appears in the Events section.
The Destroy Instance properties window appears.
The Destroy Instance window is saved and closed.
The Object Properties are saved and closed.
Now it is time to add an Instance of the button to the Room. If you’re following along with the in-software tutorial, a Room was already created for you.
To add an Instance of the button to the Room, follow these steps:
The Room Properties window for Room0 appears.
A thumbnail of the Sprite appears and the name of the Sprite appears in the field.
An Instance of the Object appears in the Room (refer to Figure 7-7).
The Room Properties window is saved and closed.
The Create Effect Action is great for simple special effects, but it’s limited to a single color at a time, and it lacks detailed customization. Because of this, in this section I show you how to use the Particles Actions. You can find them on the Extra tab of the Object Properties window.
The following procedures show you how to create a new Object that you can use to display a Catherine wheel firework (an effect that rotates quickly and produces a display of sparks and colored flame), complete with puffs of smoke.
To create a puff of smoke, follow these steps:
The Object Properties window appears.
The Create Event appears in the Events section.
A code window appears.
speed = 0;
alarm[0] = 1;
alarm[1] = room_speed * 3;
This code prevents the Object from moving and sets the necessary Alarms.
The code window is saved and closed.
Now add a Create Particle System Action to the obj_catherine_firework Object.
The Create Particle System properties window appears.
The Create Particle System Action window is saved and closed. You’ve now created a (necessary) container in which you place the particles and emitters that you want to use.
Now add a Create Particle Action, which is different from the Create Particle System Action from the last procedure.
Type 0 is the default value.
You can use a custom Sprite if you want.
These last two properties create a range of sizes between a minimum of 5 percent and a maximum of 20 percent. If you want all the particles to be the same size, use the same value for both Min and Max.
This value determines the amount the particle will increase in size per step of the game. If you don’t want a change in size, set this to 0, or use a negative number to decrease the size of the particle.
The Create Particle properties window is saved and closed.
Now add a Particle Color Action to the Object.
The Particle Color properties window appears.
This determines the particle you want to work with. In this case, you want to extend what you started with the Create Particle Action.
This creates a blend of color over each particle’s lifetime from Color1 to Color2, which you’ll set next.
The Color window appears.
This color appears as a smoke effect.
The Color window appears with a color graph.
You now have a changing color effect that blends between light gray and dark gray for the smoke effect.
The Start and End Alpha properties, as configured here, allow the particles to fade out over time.
The Particle Color properties window is saved and closed.
Now add a Particle Life Action to the Object.
This Action determines the life span of the particle.
The Particle Life properties window appears.
Setting a different value for the minimum length of the particle’s life and the maximum creates a range of time that the particle will exist.
The lifetime of a particle is calculated in steps, so these particles will be shown in the Room for a minimum of 5 steps and a maximum of 30.
The Particle Life properties window is saved and closed.
Now add a Particle Gravity Action to the Object.
The Particle Gravity properties window appears.
These settings configure the smoke to rise at a 90-degree angle.
The Particle Gravity properties window is saved and closed.
You’ve now completed creating the smoke effect. In the next section, I show you how to create the Catherine wheel firework effect.
In the preceding section, I show you how to create a smoke puff that will be attached to the firework effect that I show you how to make in this section. This time, you use Type 1 instead of Type 0, because this is the second particle effect you’re creating for this Object, obj_catherine_firework.
I highly recommend that you complete the preceding smoke trail procedure if you haven’t already done so. The following procedure builds off the smoke trail procedure and uses that effect in the firework effect.
To create the Catherine wheel firework effect, follow these steps:
The Create Particle properties window appears.
You already used Type 0 in the last procedure, so now it’s time to use Type 1.
The Create Particle properties window is saved and closed.
Now add a Particle Color Action to the Object.
The Particle Color properties window appears.
The Color window appears with a color graph.
A window with a color graph appears.
You get a mix of colors between light red and yellow.
The Particle Color properties window is saved and closed.
Now add a Particle Life Action to the Object.
The Particle Life properties window appears.
You just set the life span of the particle to last between 15 and 30 steps.
The Particle Life properties window is saved and closed.
Now add a Particle Speed Action to the Object.
The Particle Speed properties window appears.
All the particles will travel at the same speed because you set the Min and Max Speed with the same value.
Setting a minimum of 0 degrees and a maximum of 359 degrees will create a circular effect when enough particles are created at one time.
The speed of the particles will slow down at a rate of 1 pixel per step and stop when the speed reaches 0. This effectively limits the distance the particles can travel. Friction will never reduce the speed to less than 0.
The Particle Speed properties window is saved and closed.
Now add a Particle Secondary Action to the Object.
The Particle Secondary properties window appears.
The Particle Secondary properties window is saved and closed.
This procedure creates a number of secondary particles (Type 0, which is the smoke trail from the last section) when the particle reaches the end of its lifetime (the death count), which means you’ll see smoke when the sparking and flashing is done.
Now that you’ve created a smoke trail and the firework effect, you need to create an emitter, which is used to create the particles you defined earlier. Emitters can either burst or stream particles. Bursting is a one-off explosion of particles, and streaming means that particles are generated every step of the game.
To create an emitter, follow these steps:
It’s the third icon from the bottom in the left column — it looks just like the Create Particle Action.
The Create Emitter properties window appears.
The X and Y positions define the areas in which the Instances will be created. The configurations shown here provide a slight variation in the distance each particle travels.
If you want the emitter to move with an Instance, place this Action in a Step Event of the Instance so that the Emitter position is updated every step.
The Set Alarm Action properties window appears.
You just set an Alarm Action to trigger Alarm 0 for every step. You set the Alarms in the following procedures.
At this point, your Actions for the Create Event should look like Figure 7-8.
Now you need to add an Alarm[0] Event to control the Create Event. To create the Alarm Event, follow these steps:
The Burst From Emitter properties window appears.
Remember that Type 1 is the Catherine wheel effect from earlier in this chapter.
This will burst 50 particles of Type 1.
The Burst From Emitter properties window is saved and closed.
Now add a Set Alarm Action to the Object.
The Set Alarm properties window appears.
The Set Alarm properties window is saved and closed. You just set an Action to trigger Alarm 0 once for every five steps of the game.
Now add an Alarm[1] Event to the Object.
The Alarm 1 Event appears in the Events section.
The Set Alarm properties window appears.
A negative number essentially turns off the Alarm selected, which means the Event won’t be triggered.
The Set Alarm Action window is saved and closed. You just set an Action to turn off Alarm 0.
Now add another Set Alarm Action to the Object.
The Set Alarm properties window appears.
Now Alarm 2 will be triggered twice every step.
The Set Alarm properties window is saved and closed.
Now add an Alarm[2] Event to the Object.
The Alarm 2 Event appears in the Events section.
The Destroy Emitter properties window appears.
The Destroy Emitter properties window is saved and closed. You just set an Action to destroy the emitter when Alarm 2 is triggered.
The Destroy Particle System Action appears in the Actions section. There is nothing to set here — you’re essentially creating an Action to destroy the particle system when Alarm 2 is triggered.
The Destroy Instance properties window appears.
This Alarm will stop the effect and destroy the Emitter, the Particle System, and the Instance.
The Destroy Instance properties window is saved and closed.
The Object Properties window is saved and closed.
Your Alarm 2 Event should look like Figure 7-9.
With these procedures complete, you can move on to create a button so that the player can trigger this firework. You can duplicate the button you made for the Create Effect Action from the beginning of this chapter to create a new button quickly.
To create a button to trigger the Catherine wheel effect, follow these steps:
A duplicate of the Object is created and appears in the Resource tree as Object2, or something similar. The Object Properties window also appears.
A thumbnail of the Sprite appears and the name of the Sprite appears in the field.
The Step Event Actions appear in the Actions section.
The code window appears.
if instance_position(mouse_x, mouse_y, id)
{
if image_index != 2
{
image_index = 1;
if mouse_check_button_pressed(mb_left) && ! instance_exists(obj_catherine_firework)
{
alarm[0] = room_speed / 2;
image_index = 2;
instance_create(random(room_width), 350, obj_catherine_firework);
}
}
}
else
{
if image_index != 2
{
image_index = 0;
}
}
This code is basically the same code as the previous button, but it allows a single Instance of the firework at a time. Go ahead and place an Instance of the button in the Room now and run the game.
When you test the game now, you might notice that you can only click the Catherine wheel button (labeled D’n’D) once and then you have to wait for the effect to finish before you can trigger it another time. For the Built In Particles effect, you can click that button multiple times to have the effect appear multiple times.
3.142.135.249