Code - Preview
Code - Preview
Drag and drop an Execute Code Action from the Control tab to the
Actions section.
A code window appears.
5. In the code window, type the following:
surf = -1;
6. Click the green check mark.
The code window is saved and closed.
7. Choose Add Event➪Other➪Room End.
The Room End Event appears in the Events section.
8. Drag and drop an Execute Code Action from the Control tab to the
Actions section.
A code window appears.
9. In the code window, type the following:
if surface_exists(surf)
{
surface_free(surf);
}
10. Click the green check mark.
The code window is saved and closed.
11. Choose Add Event➪Draw➪Draw.
The Draw Event appears in the Events section.
12. Drag and drop an Execute Code Action from the Control tab to the
Actions section.
A code window appears.
13. In the code window, type the following:
if !surface_exists(surf)
{
surf = surface_create(room_width,room_height);
surface_set_target(surf);
draw_clear_alpha(c_black,0);
surface_reset_target();
surface_reset_target();
view_surface_id[0] = surf;
}
14. Click the green check mark.
The code window is saved and closed.
197
Chapter 9: Using Surfaces to Create Effects
Much of the preceding code is covered in detail earlier in this chapter.
However,
the last line of this code block creates a variable that holds the Surface ID
that
GameMaker will draw to. Now, whenever view[0] is drawn, it will be drawn
to
the Surface and not the screen.
If you add this Object to the Room now, you won’t see anything different,
because you haven’t created any effects for the Surface. You can do that in
the next section.
Creating the Surface View effect
In the preceding section, you set up the Room and an Object so that you can
have the View drawn on the Surface for the purpose of displaying effects. In
this section, you create the effect that will be drawn on the Surface.
The following procedures continue with the in-software tutorial Surfaces_
Part2. Here, you create a shock wave effect that is shown with every explo-
sion in the game.
To create the Object for the shock wave effect, follow these steps:
1. With the Surfaces tutorial open, from the Resource tree, right-click
Objects and select Create Object.
The Object Properties appear.
2. In the Name field, type obj_Shockring.
3. In the Depth field, type –800.
The shock wave effect appears over most of the Instances in the game,
but not all of them.
4. Choose Add Event➪Create.
The Create Event appears in the Events section.
5. Drag and drop an Execute Code Action from the Control tab to the
Actions section.
A code window appears.
6. In the code window, type the following:
ww = 50; // width of the ring effect
radius = 0; // size check variable
r_max = 120; // maximum radius of the effect
spd = 1; // expansion speed
d = 360 / 15; // ring segments to be drawn
if instance_exists(obj_Effect_Surface)
{
tex = surface_get_texture(obj_Effects_Surface.surf);
For information on how to create controls for mobile devices that don’t have
a keyboard, check out Chapter 13.
Coding keyboard controls
You can use the drag-and-drop Actions (see Chapter 4) to create keyboard
controls, but you can also write your own code for keyboard controls. The
following procedures show you how to write code to make an Object move
left and right.
To code left and right keyboard controls, follow these steps:
1. Create a Sprite that will represent what the player controls, such as
the bat Sprite from the breakout tutorial.
See Chapter 2 for more information on creating Sprites.
The Sprite should appear in the Resource tree.
2. Choose Resources➪Create Object.
The Object Properties window appears.
3. In the Name field, type something like obj_bat.
4. From the Sprite drop-down list, select the Sprite, such as spr_bat (see
Chapter 2).
A thumbnail of the image appears in the Sprite section, and the name of
the image appears in the field.
5. Choose Add Event➪Keyboard➪Left.
A Left Keyboard Event appears in the Events section of the Object
Properties.
6. Drag and drop the Execute Code Action from the Control tab into the
Actions section.
An empty code window appears.
7. In the code window, type the following code:
if place_meeting(x - 5, y, obj_wall) = false
{
x -=5;
}
The code first checks for a collision between the Object (an Instance
of the Object during gameplay) and obj_wall (if there are fewer than 5
pixels separating the Object from the wall). If there is less than 5 pixels
of space between the Object and the wall, GameMaker sets the X posi-
tion of the Object to move 5 pixels over. In that way, the player can’t
move the Object into the wall.
229
Chapter 11: Coding with GameMaker Language
8. Click the green check mark.
The code window is saved and closed.
9. Choose Add Event➪Keyboard➪Right.
The Right Keyboard Event appears in the Events section.
10. From the Control tab, drag and drop an Execute Code Action into the
Actions section.
An empty code window appears.
11. In the code window, type the following code:
if place_meeting(x + 5, y, obj_wall) = false
{
x +=5;
}
The code for the Right arrow key is the same as for the Left arrow key,
except you’re checking for the right boundary (in this case, obj_wall). A
good thing to remember is that +x is to the right and –x is to the left. In
the same way, +y is down and –y is up.
12. Click the green check mark.
The code window is saved and closed.
You could test this code now by placing an Instance of the bat Object in the
Room. Of course, you would need the wall Object created as well. To find out
how to the create the wall Object, you can follow along with GameMaker’s
coding tutorial.
To code the spacebar to trigger an Action, follow these steps:
1. With your Object open, such as the Object from the last procedure,
choose Add Event➪Keyboard➪Space.
The Space Keyboard Event appears in the Events section.
2. Drag and drop an Execute Code Action from the Control tab to the
Actions section.
An empty code window appears.
3. Type the following code:
with (obj_ball)
{
if go = false
{
go = true;
direction = 45 + random(90);
speed = 5;
}
}