Game Development Reference
In-Depth Information
4.
Open
obj_explorer
, select the
Step
event, and double-click the
Execute Code
action.
Change it to read the following:
1:
{
2:
if ( !place_snapped(32,32) ) exit;
3:
var dx, dy;
4:
dx = keyboard_check(vk_right)-keyboard_check(vk_left);
5:
dy = keyboard_check(vk_down)-keyboard_check(vk_up);
6:
if ( dx != 0 ) dy = 0;
7:
if ( place_meeting(x+dx*32,y+dy*32,obj_wall) )
8:
speed=0
9:
else
10:
{
11:
hspeed = 4*dx;
12:
vspeed = 4*dy;
13:
}
14:
box = instance_place(x+dx*32,y+dy*32,obj_box);
15:
with ( box )
16:
{
17:
if ( instance_place(x+dx*32,y+dy*32,obj_impassable) )
18:
other.speed = 0
19:
else
20:
{
21:
hspeed = other.hspeed;
22:
vspeed = other.vspeed;
23:
alarm[0] = 32/speed;
24:
}
25:
}
26:
if ( speed!=0 )
27:
{
28:
switch ( direction )
29:
{
30:
case 0: sprite_index = spr_explorer_right; break;
31:
case 90: sprite_index = spr_explorer_up; break;
32:
case 180: sprite_index = spr_explorer_left; break;
33:
case 270: sprite_index = spr_explorer_down; break;
34:
}
35:
image_speed = 0.5;
36:
} else image_speed = 0;
37:
}
It's a long script, but if you have been reading Grid Movement, then most of it should
already make sense. Lines 2-13 deal with
obj_explorer
's regular grid movement: check
if it is aligned with the grid, calculate the direction to move into based on the keyboard
input, make sure that it moves either horizontally or vertically, but not both, and that
isn't trying to run into walls. At the end, lines 26-36 set the sprite and animation speed
dependent on the speed and direction.
Search WWH ::
Custom Search