Enemy Shooting GLITCH

M

Marko Kajtazi

Guest
Hey everyone,
I made a nice enemy state machine for scouting and shooting. When it is in scouting mode it just moves around, but when it gets in shooting mode it starts to shoot in it's direction. The problem is when it sets back to the scouting state it continues to shoot while moving.
Here is the code:
Code:
// Create Event
vspd = 0;
grav = 1;

hspd = 0;
dir = 1;
acc = 2;
dis_to_plater = noone;
movespeed = 10;
max_movedelay = 30;
movedelay = max_movedelay;

bullet = noone;
hp = 2;

state = "scout";
Code:
// Step Event
dis_to_plater = sign(oPlayer.x - x);

switch(state)
{
    case "scout": {
        with(bullet) {
            instance_destroy();
        }
       
        if(movedelay == 0) {
            dir *= -1;
            movedelay = max_movedelay;

        }

        hspd = dir * movespeed;
        vspd += grav;
        movedelay--;
       
        if((dis_to_plater == dir) && (oPlayer.y == y)) {
            hspd = 0;
            state = "attack";
        }
   
    }
   
    case "attack": {
        bullet = instance_create_layer(x + dir * 20, y, "Bullet", oBullet_enemy);
       
        with(bullet) {
            speed = 6;
           
            if(oEnemy.dir == 1) {
                direction = 0;
            }
           
            if(oEnemy.dir == -1) {
                direction = 180;
            }
        }
       
        if(!(dis_to_plater == dir) || !(oPlayer.y == y)){
            state = "scout";
        }
    }
}
Can anyone help me?.
Thanks.
 

Relic

Member
You forgot to put a “break;” in between the cases to tell the code where to stop running the case when you are scouting.

At the moment when state is “scout” it will run all your scout code, but reaching case “attacking” isn’t telling your code to stop, so all the attacking code runs as well.
 
M

Marko Kajtazi

Guest
You forgot to put a “break;” in between the cases to tell the code where to stop running the case when you are scouting.

At the moment when state is “scout” it will run all your scout code, but reaching case “attacking” isn’t telling your code to stop, so all the attacking code runs as well.
Of course,
Now it's working perfect.
Thanks!!
 
Top