Game Development Reference
In-Depth Information
2.
The beastie object will still inherit its
Destroy
event from its parent so open
obj_enemy
and select its
Destroy
event. Now observe the difference in the following lines of code:
1:
var instance;
2:
instance = instance_create( x, y, obj_enemy_die );
3:
instance.speed = 4;
4:
instance.direction = random( 180 );
5:
instance.sprite_index = die_sprite_index;
6:
instance.image_index = random( instance.image_number-1 );
This now extends the principle of the
Create Moving
code to change the
sprite_index
and
image_index
of the new instance as well. In fact, you could set any variables you
like on a new instance in this way, but bear in mind that these are set after the
Create
event of the new instance is called. In other words the
Create
event called as part of
line 2 can't rely on values that are not set until lines 3-6.
3.
The
Create
event of
obj_enemy_die
now doesn't need to handle anything to do with the
sprite—and we've avoided the use of a global variable.
Switching Sprites
It's understandable if the thought of opening the
Draw
event of
obj_zool
fills you with a certain
amount of dread. Although it grew over a long period of time, the final event contained a quite
ridiculous 53 actions! Moreover, this event only ever
drew
the correct sprite for the current
state, and it never actually
changed
the current sprite (by using
Set Sprite
or setting the
sprite_index
variable). This meant that Game Maker wasn't actually aware that the sprite was
changing, which could potentially have caused animation and collision problems. If Game
Maker doesn't know how many frames there are in the sprite being drawn, then it might not
l oop the a n i ma ti on cor r e ctl y , a n d i t won ' t be usi n g the cor r e ct col l i si on ma sk e i the r (a l thoug h
our masks were all the same for Zool, which is why it didn't cause a problem). We could have
fi xe d thi s by addi n g an othe r
Set Sprite
action for each condition, but this would have increased
the number of actions to nearly 200 (because we would have needed
Start
and
End Block
actions, too). Clearly that wasn't a great alternative, but fortunately there are much better ways
of doing this in GML.
Within zooldrag.gmk
1.
Open up the
Draw
event of
obj_zool
and look at the code in the
Execute Code
action.
The direct conversion of the D&D is still pretty long, but looks a little bit neater. You'll
see variations of the following lines repeated over and over again:
if( state == ZSTATE_STAND )
draw_sprite( spr_zool_stand_right, -1, x, y );
Nonetheless, it still doesn't set the
sprite_index
variable, so it remains vulnerable to
the same animation and collision problems as the D&D version.
Search WWH ::
Custom Search