Game Development Reference
In-Depth Information
Left and Right
The basic problem with horizontal lift segments is that we expect them to move together in a long
line, but the segment at the back of the line has no idea when the segment at the front of the line
has collided with
obj_solid
. Our lift segments are children of
obj_solid
themselves, so you might
think that when the segment at the front stops, the next segment would collide into it, and so on,
until all the segments come to a halt. This is kind of true, but these collisions would actually
happen in subsequent steps, producing a messy “train crash” effect. As a result, the
Alarm
event
would get set in different steps and the line of segments would start breaking apart as they began
moving again (try it if you like).
So what we need is the ability to instantly let all the segments in the line know that there has
been a collision, so that they all can stop moving in the same step. One potential way to do this
would be to apply a
Set Alarm
action to all lift objects at the same time (just by using the
Applies
to
option on the top of the action form). Unfortunately, this would mean that all the lifts in the
same room would receive the alarm and come to a halt, so unless we only have one horizontal lift
on each level, then that won't work either.
A better solution is to use a
loop
to let all the adjacent segments know about the collision.
We've used a
Repeat
action before to create multiple
obj_enemy_die
objects when an enemy dies,
but it has all sorts of uses. We will use it here to repeatedly check along a line looking for other
instances of the same lift object that are linked to the one that has collided. We then just need to
change each one into an instance of the lift object that moves in the opposite direction.
Unfortunately, although Game Maker provides a
Check Object
action that we could use to
check for adjacent instances, there is no way of calling
Change Instance
on that adjacent
instance. What we need is an equivalent of the
Check Object
action that also provides us with the
actual instance found. We then need an equivalent of the
Change Instance
action that can be
called for another instance. Fortunately, both of these are easy to create using scripts that we can
then apply in the horizontal lift objects to achieve the desired effect.
Creating Horizontal Lifts
1.
Use the
Import Scripts
option (
Scripts
menu) to load
check_object_returned.gml
and
change_instance_for.gml
from the
Chapter06/Resources
directory on the CD. This will
add these scripts to the
Scripts
folder in the resource tree.
2.
Create a new object called
obj_lift_hlimit
(in the
Platforms
,
Lifts
group), give it the
spr_lift_hlimit
sprite, and disable the
Visible
option.
3.
Create two new objects called
obj_lift_left
and
obj_lift_right
. Give them the
appropriate lift sprites, set their
Depth
to
-1001
, and their
Parent
to
obj_lift
.
4.
Add a
Create
event to
obj_lift_left
and include a
Speed Horizontal
action (
move
tab) that sets
Hor. Speed
to
0
. The horizontal platforms will also stand still for a while
as they change direction.
Include a
Set
Alarm
action (
main2
tab) that sets
Alarm 0
to
90
Steps
.
5.
6.
Add an
Alarm, Alarm 0
event and include a
Speed Horizontal
action (
move
tab) that
sets
Hor. Speed
to
-LIFT_SPEED
.
7.
Add a
Collision
event with
obj_solid
and include a
Set Variable
action (
control
tab)
that sets
horizontal_offset
to
16
. This is a new variable that we will use to keep track
of how far to the left or right we are looking for platform segments.
Include a
Repeat
action with
Times
set to
10
. Setting this number to 10 means that we
will stop looking for adjacent platform segments after we have found 10—so platforms
can't be longer than 10 segments. If you want longer platforms, then increase this.
8.
Search WWH ::
Custom Search