Z
Zack Garrison
Guest
I have just completed the wonderful GML2 Complete Platformer Tutorial series by Shaun Spalding on Youtube. The code is identical to how he set up his simple shooter, I have just changed the art and little details and values here and there, and everything works!
However, my enemies do not have guns that they "hold". I instead have fireballs that shoot from the enemy's mouth. The gun object is a tiny, red, transparent square on the head of the enemy. It detects where the player is, aims, and fires appropriately.
I have a second enemy sprite that preforms the action of shooting a fireball. The problem is that I can't figure how to switch to the attack_fireball sprite, shoot the fireball, then switch back to the enemy_run sprite and have it continue running around until it wants to attack again.
Code is as follows:
In (obj_enemy > create event), I have the following code that creates the tiny, red square ("obj_invisible_gun") on the head area of the enemy instances where the "hasweapon" variable is true.
if (hasweapon){
mygun = instance_create_layer(x,y,"Gun",obj_invisible_gun)
with (mygun){
owner = other.id;
}
} else mygun = noone;
In (obj_invisible_gun > step event) the invisible gun finds the player if within a certain radius and shoots a fireball if there are no walls in between them.
if (instance_exists(obj_player)){
if (obj_player.x < x) image_yscale = -image_yscale;
if (point_distance(obj_player.x,obj_player.y,x,y) < 600){
image_angle = point_direction(x,y,obj_player.x,obj_player.y);
countdown--;
if (countdown <= 0){
countdown = countdownrate;
if (!collision_line(x,y,obj_player.x,obj_player.y,obj_wall,false,false)){
attacking = true;
audio_sound_pitch(snd_gun3,choose(0.8,1.0,1.2));
audio_play_sound(snd_gun3,5,false);
//Shoot
with(instance_create_layer(x,y,"Bullets",obj_fireball)){
image_speed = 1;
spd = 5;
//Offset direction of bullet
direction = other.image_angle + random_range(-3,3);
image_angle = direction;
}
}
}
}
}
I've been trying different "with" statements, and moving code into different objects, grabbing IDs and I even tried creating an "enum" function for different states of an enemy but I almost screwed everything up by forgetting what I changed!! LOL!
I just can't seem to find the right combination and order of code to have an enemy see a collision path to the player with their invisible gun, stop moving, change to the attack sprite, shoot the fireball with their invisible gun, then return to the run sprite and resume it's horizontal and vertical speeds.
Any tips or advice?
Thanks
Zack
However, my enemies do not have guns that they "hold". I instead have fireballs that shoot from the enemy's mouth. The gun object is a tiny, red, transparent square on the head of the enemy. It detects where the player is, aims, and fires appropriately.
I have a second enemy sprite that preforms the action of shooting a fireball. The problem is that I can't figure how to switch to the attack_fireball sprite, shoot the fireball, then switch back to the enemy_run sprite and have it continue running around until it wants to attack again.
Code is as follows:
In (obj_enemy > create event), I have the following code that creates the tiny, red square ("obj_invisible_gun") on the head area of the enemy instances where the "hasweapon" variable is true.
if (hasweapon){
mygun = instance_create_layer(x,y,"Gun",obj_invisible_gun)
with (mygun){
owner = other.id;
}
} else mygun = noone;
In (obj_invisible_gun > step event) the invisible gun finds the player if within a certain radius and shoots a fireball if there are no walls in between them.
if (instance_exists(obj_player)){
if (obj_player.x < x) image_yscale = -image_yscale;
if (point_distance(obj_player.x,obj_player.y,x,y) < 600){
image_angle = point_direction(x,y,obj_player.x,obj_player.y);
countdown--;
if (countdown <= 0){
countdown = countdownrate;
if (!collision_line(x,y,obj_player.x,obj_player.y,obj_wall,false,false)){
attacking = true;
audio_sound_pitch(snd_gun3,choose(0.8,1.0,1.2));
audio_play_sound(snd_gun3,5,false);
//Shoot
with(instance_create_layer(x,y,"Bullets",obj_fireball)){
image_speed = 1;
spd = 5;
//Offset direction of bullet
direction = other.image_angle + random_range(-3,3);
image_angle = direction;
}
}
}
}
}
I've been trying different "with" statements, and moving code into different objects, grabbing IDs and I even tried creating an "enum" function for different states of an enemy but I almost screwed everything up by forgetting what I changed!! LOL!
I just can't seem to find the right combination and order of code to have an enemy see a collision path to the player with their invisible gun, stop moving, change to the attack sprite, shoot the fireball with their invisible gun, then return to the run sprite and resume it's horizontal and vertical speeds.
Any tips or advice?
Thanks
Zack