Introduction To Game Design & Programming in GameMaker Studio 2
Page 10
Figure 7_36 shows the sprite used for this object:
Figure 7_36: Showing sprite spr_fly_enemy_1
Create Event
/// @description Set Up
start_hp=global.level*global.level*2;
hp=start_hp;
hspeed=-4;
on_path=false;
y=irandom_range(200,room_height-200);
alarm[0]=room_speed*4;
Sets up initial hp values, a horizontal speed of -4 (moves to the left) and a flag for on_path.
y=irandom_range(200,room_height-200);
It’s y position (up and down location) is set as a random value between 200 and the room’s height less 200.
An alarm 0 is set for 4 seconds.
Step Event
/// @description Check Health and Start Path
if x<0 instance_destroy();
//if hp<1 && image_index==14
if hp<1
{
instance_destroy();
coin=instance_create_layer(x,y,"Player",obj_coin_bubble);
coin.value=start_hp;
for (var i = 0; i < 11; i += 1)
{
part=instance_create_layer(x,y,"Score_Effect",obj_air_parts);
part.image_index=i;
}
}
if on_path==false && x
{
on_path=true;
path_start(path_circle,4,path_action_restart,false);
}
The first part of this code works in a similar way to the code in obj_enemy_floor_1.
if on_path==false && x
{
on_path=true;
path_start(path_circle,4,path_action_restart,false);
}
This will check if it is currently moving on a path, if it is not and the y position is less than 400 from the right edge it will process the code block. The code block changes the flag for on_path so it won’t trigger again and start moving on the defined path.
Alarm 0 Event
/// @description Create a Proctile
alarm[0]=room_speed*3;
arrow=instance_create_layer(x,y,"Enemy_Missile",obj_enemy_arrow_1);
ang=point_direction(x,y,obj_player.x,obj_player.y);
arrow.direction=ang;
arrow.image_angle=ang;
arrow.speed=12;
arrow.strength=global.level;
Restarts the alarm, creates an projectile that moves to the player’s current location.
Draw Event
/// @description Draw Self & healthbar
draw_self();
scr_healthbar(30,-75,60);
Draws self and a health bar.
Collision With obj_mine_parent Event
/// @description Reduce hp
hp-=other.my_power;
Creates damage if in collision with one of the player’s mines (when present).
Collision With obj_player_weapon_parent Event
/// @description REduce hp
hp-=other.my_power;
with other instance_destroy();
audio_play_sound(snd_exp_1,1,false);
Reduces hp based on strength of player’s projectile, destroys the projectile and plays a sound.
Collision With obj_power_bullet Event
/// @description reduce hp
hp-=other.my_power/10;
Reduces hp by 1/10 the value of the projectile.
obj_enemy_arrow_1
This is a projectile that will cause damage to player if they collide it.
Figure 7_37 shows the sprite setup for this object.
Note: It is pointing to the right. In GMS2, right is equal to 0’, so when moving it will point in the correct direction.
Figure 7_37: Showing sprite set up for spr_enemy_arrow
Collision With obj_mine_parent Event
/// @description destroy
instance_destroy();
Destroys self if in collision with the player’s mines.
Outside Room Event
/// @description destroy
instance_destroy();
Prevent a memory leak by destroying when no longer needed.
obj_boss
This boss object will follow a path, and then move up and down.
Figure 7_38 shows the sprite set up for this object.
Note: The sprite origin is the location that we will be fixing the boss’s weapon to.
Figure 7_38: Showing the sprite for obj_boss
Create Event
/// @description set up
path_start(path_boss,4,path_action_stop,true);
instance_create_layer(x,y,"Enemy_Cannon",obj_cannon);
instance_create_layer(x,y,"Enemy_Cannon",obj_boss_hit);
start_hp=5*global.level*global.level;
hp=start_hp;
hit=false;
flash=false;
This sets the boss object to start moving on the defined path and create instances of obj_cannon and obj_boss_hit. It also sets up the initial hp based on the current games level. It will also set a flag for hit and flash, which will be used to graphically show damage.
Step Event
/// @description Check hp & for hit
if hp<0
{
coin=instance_create_layer(x,y,"Player",obj_coin_bubble);
coin.value=start_hp;
with (obj_boss_hit) instance_destroy();
with (obj_cannon) instance_destroy();
instance_destroy();
for (var i = 0; i < 10; i += 1)
{
part=instance_create_layer(x,y,"Score_Effect",obj_boss_parts);
part.image_index=i;
}
}
if hit==true
{
hit=false;
flash=true;
alarm[0]=room_speed;
}
Checks the hp, creates a coin instance if it is dead, destroys itself and creates instances for the explosion effect. Sets flags needed to show damage.
Alarm 0 Event
/// @description Turn off flash
flash=false;
Resets the flag for flash to false.
Draw Event
/// @description Drawing stuff
scr_healthbar(-20,-70,80);
if flash==true
{
draw_sprite_ext(sprite_index,image_index,x,y,1,1,0,c_red,1);
}
else
{
draw_self();
}
Draws a health bar, and then draws the assigned sprite with a red hue if to show damage, or draws normally.
Collision With obj_player_weapon_parent Event
/// @description destroy other
with (other) instance_destroy();
Destroys the colliding instance – note no damage is recorded or acted upon.
Intersect Boundary Event
/// @description Change vert direction
vspeed=vspeed*-1;
Changes the vertical speed to move up / down on collision with border.
Path Ended Event
/// @description Set moving
vspeed=4;
When the boss initially reaches the end of the path, this code will make it start moving down at a speed of 4 pixels per step.
obj_boss_hit
This is the object instance that the player hit with their weapons to make damage to the boss enemy.
Note: In the object settings, the visible checkbox has been unchecked.
Figure 7_39 shows the sprite used for this instance.
Figure 7_39: Showing sprite for obj_boss_hit
Step Event
/// @description Keep in sync with boss
x=obj_boss.x+36;
y=obj_boss.y+70;
This keeps it’s location relative the boss, so when the boss moves, so does the target.
Collision With obj_player_weapon_parent Event
/// @description Make Damage
obj_boss.hp-=5;
obj_boss.hit=true;
/>
with (other) instance_destroy();
Reduces the boss’s hp and set’s flag to true so it shows damage, destroys colliding projectile.
Note: Addressing an object like this should only be done when you know that there is only one instance of the object you are referencing present in the game’s room.
obj_canon
This obect moves the boss enemy, and peridocally fires a projectile towards the player.
Figure 7_40 shows the sprite for this object.
Note: It is upside down so when it rotates, for example by 180’, it points in correct direction and the has correct orientation.
Figure 7_40: Cannon sprite assigned.
Create Event
/// @description Set Up
image_speed=1;
Sets the initial speed that the frames will animate by.
Step Event
/// @description Point to player, fire projectile, sync with boss
image_angle=point_direction(x,y,obj_player.x,obj_player.y);
if image_index==5
{
alarm[0]=room_speed*2;
ball=instance_create_layer(x,y,"Enemy_Ball",obj_ball);
ball.direction=image_angle;
ball.speed=12;
}
x=obj_boss.x;
y=obj_boss.y;
image_angle=point_direction(x,y,obj_player.x,obj_player.y);
This line keeps it pointing at the player’s location.
if image_index==5
{
alarm[0]=room_speed*2;
ball=instance_create_layer(x,y,"Enemy_Ball",obj_ball);
ball.direction=image_angle;
ball.speed=12;
}
This sets an alarm for two seconds, and fires a ball instance towards the player’s current location.
x=obj_boss.x;
y=obj_boss.y;
This keeps the canon relative to the boss object.
Alarm 0 Event
/// @description Set Image Speed
image_speed=1;
Sets the image speed to 1, normal animation.
Animation End Event
/// @description Stop Anitmating
image_speed=0;
Stops the sprite from animating when the final frame is reached, the alarm will make it animate again when triggered.
obj_ball
The previous object fires this projectile towards the player’s current location.
Figure 7_41 shows the sprite for this object:
Figure 7_41: Showing sprite spr_ball
Collision With obj_mine_parent Event
/// @description Destroy
instance_destroy();
Destroys self if collided a player’s mine.
Outside Room Event
/// @description Destroy
instance_destroy();
Prevents memory issues by destroying when done with.
obj_en_parent_projectile
There is no code or sprite for this object
obj_floor_parts
Figure 7_42 shows sprite for this object:
When the floor enemy has been destroyed, this object creates a cool explosion effect.
Figure 7_42: Sprite spr_floor_parts
Create Event
/// @description Set Moving
image_speed=0;
rot=choose(-4,4);
direction=irandom(360);
speed=4+irandom(4);
size=0.2;
alpha=1;
Sets the image speed to 0 to prevent animation. Sets to rotate clockwise or anti-clockwise, a random direction and a speed between 4 and 8. Sets the initial size as 20% and full visibility.
Step Event
/// @description Rotatate and change size and alpha
image_angle+=rot;
size+=0.01;
image_xscale=size;
image_yscale=size;
if size>1 alpha-=0.02;
image_angle+=rot;
size+=0.01;
image_xscale=size;
image_yscale=size;
This code rotates the image and slowly increases the size.
if size>1 alpha-=0.02;
Starts lowering the value of alpha when size is over 1.
Draw Event
/// @description Draw
image_alpha=alpha;
draw_self();
image_alpha=1;
Sets the alpha value, draws the image, then resets alpha so other object instances are not affected.
Outside Room Event
/// @description Destroy
instance_destroy();
Destroys when outside room.
obj_air_parts
When the air enemy has been destroyed, this object creates a cool explosion effect.
Figure 7_43 shows sprite set up for this object:
Figure 7_43: Showing the sprite set up for spr_air_parts
Code below is not explained, as it is the same as for obj_floor_parts.
Create Event
/// @description Set up
image_speed=0;
rot=choose(-4,4);
direction=irandom(360);
speed=4+irandom(4);
size=0.2;
alpha=1;
Step Event
/// @description Scale & set alpha
image_angle+=rot;
size+=0.01;
image_xscale=size;
image_yscale=size;
if size>1 alpha-=0.02;
Draw Event
/// @description Drawing Stuff
image_alpha=alpha;
draw_self();
image_alpha=1;
Outside Room Event
/// @description destroy
instance_destroy();
obj_boss_parts
When the boss enemy has been destroyed, this object creates a cool explosion effect.
Figure 7_44 shows sprite set up for this object:
Figure 7_44: Showing the sprite set up for spr_air_parts
Code below is not explained, as it is the same as for obj_floor_parts.
Create Event
/// @description Set up
image_speed=0;
rot=choose(-4,4);
direction=irandom(360);
speed=4+irandom(4);
size=0.2;
alpha=1;
Step Event
/// @description Uddate
image_angle+=rot;
size+=0.01;
image_xscale=size;
image_yscale=size;
if size>1 alpha-=0.02;
Draw Event
/// @description Drawing code
image_alpha=alpha;
draw_self();
image_alpha=1;
Outside Room Event
/// @description Destroy
instance_destroy();
obj_sheild_collect
This is an object that moves across the window, and makes the player temporary invincible.
Figure 7_45 shows the sprite for this object:
Figure 7_45: Sprite for obj_shield_collect
Create Event
/// @description Start Movin
hspeed=-5;
Sets the instance moving to left at a speed of 5.
Step Event
/// @description destroy
if x<0 instance_destroy();
Destroys self when off the left side of the window.
obj_power_up
Moves across the screen and increases the level and upgrades weapons, when collected by the player.
Figure 7_46 shows sprite spr_power_up:
Figure 7_46: Showing spr_power_up used for obj_power_up
The code does the same as the previous object, so is not explained.
Create event
/// @description Start moving
hspeed=-6;
Step Event
/// @description Destroy
if x<0 instance_destroy();
obj_coin_bubble
Player can collect score points by colle
cting instances of this object.
Figure 7_47 shows the sprite set up for this object:
Figure 7_47: Showing the sprite spr_coin_bubble
Create Event
/// @description Move towards player location
motion_set(point_direction(x,y,obj_player.x,obj_player.y),5);
This code sets the instance to move towards the player’s current location at a speed of 5.
Collison With obj_player Event
/// @description Start Score Effect
audio_play_sound(snd_coin,1,false);
ins=instance_create_layer(x,y,"Score_Effect",obj_to_score);
ins.value=value;
instance_destroy();
When it collides with the player, it play’s a sound and makes an instance of obj_to_score and gives it a value value. It then destroys itself.
Outside Room Event
/// @description Destroy
instance_destroy();
Destroys if not collected by the player, once off the left side of the window.
obj_to_score
Increases the score once near the drawn score on the HUD.
There is no sprite for this instance.
Create Event
/// @description Move towards score on HUD
dir=point_direction(x,y,200,40);
motion_set(dir,4);
Moves towards point 200,40 (which is the point on the window where the score is drawn).
Step Event
/// @description Check location
if distance_to_point(200,40)<8
{
instance_destroy();
score+=value;
}
When close to the score location, destroys self and increases the score.
Draw Event
/// @description Draw Value
draw_text(x,y,value);
Draws it’s value.
obj_asteroid_large
A large asteroid that the player can shoot at. If shot it creates smaller asteroids. Damages player if player hits it.