Game Development Reference
In-Depth Information
It's time to deal with death... You might be tempted to simply use a
Collision
event and make
the explorer die upon contact with a scorpion, but in grid-based games, player characters
typically die when there is considerable overlap between the player sprite and his enemies,
sometimes only when they both fully occupy the same square. We could do this by changing the
collision masks, but it's just as easy to build a check in the collision event.
Dealing with Collisions
1.
Create an object
obj_hat
and set the
Sprite
to
spr_hat
. We'll drop this hat where the
explorer dies.
2.
Add a
Create
event and include an
Align to Grid
action with
Snap hor
and
Snap vert
both set to
32
. This makes sure that wherever we drop the hat once the explorer dies, it
always aligns to the grid.
3.
Double-click
obj_explorer
and add a
Collision
event for
obj_scorpion
. Include an
Execute Code
action and insert the following lines:
1:
{
2:
if ( abs(x-other.x) <= 16 && abs(y-other.y) <= 16 )
3:
{
4:
instance_create(x,y,obj_hat);
5:
with ( other ) instance_destroy();
6:
instance_destroy();
7:
}
8:
}
Line 2 checks the horizontal and vertical distance between
obj_explorer
and its
colliding enemy. Only if they are both smaller than
16
(half of both sprites) do we
register the kill. The lower the values you use, the closer the instances must be
together, and If you set them at 0 then they must be lined up exactly. Lines 4-6 drop
the hat and kill both instances in the collision. It doesn't matter if the explorer was
halfway two squares as the hat drops itself neatly in the nearest square.
Result:
Reference/Result/grid_movement3.gmk
Search WWH ::
Custom Search