Game Development Reference
In-Depth Information
Implementing the Follow Behavior for the Parrot Object
1.
Open the
Create
event of
obj_parrot
(in the
Flynn
group) and edit the
Execute Code
action. Include additional lines of code that create three new instance variables:
target_id
set to
noone
and both
targetx
and
targety
set to
0
. We will use these
variables to keep track of what instance Archie is following (if anything) and the point
he is moving toward.
2.
Add a
Step, Step
event and include an
Execute Code
action containing the following:
1:
{
2:
switch( state )
3:
{
4:
case ASTATE_FOLLOW:
5:
break;
6:
case ASTATE_COMMAND:
7:
break;
8:
case ASTATE_DISTRACT:
9:
break;
10:
}
11:
}
This is the basic framework for constructing a state machine using a
switch
statement,
and we will use it as the basis for creating Archie's different behaviors. Each state has
its own
case
statement that handles behaviors specific to that state (usually including
state transitions). Anything common to all states can appear outside of the
switch
statement, either before or after it, depending on the situation.
3.
Now, expand the
case
statement for
ASTATE_FOLLOW
to include the following code (note
you do not need to include lines 1 and 11 again, as they should already be there):
1:
case ASTATE_FOLLOW:
2:
3:
targetx = obj_flynn.x - (obj_flynn.facing*64);
4:
targety = obj_flynn.bbox_top;
5:
6:
if( x > obj_flynn.x )
7:
facing = FACE_LEFT;
8:
else
9:
facing = FACE_RIGHT;
10:
11:
break;
Lines 3 and 4 set the target position for Archie to be just above and behind Flynn's
shoulder depending on the direction in which he is facing. Lines 6-9 make sure that
Archie always looks at Flynn as he is following him around.
Search WWH ::
Custom Search