1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 Need help with enemy AI

Discussion in 'Programming' started by pixeltroid, Mar 10, 2019.

  1. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    603
    Basically I have an enemy that run towards player and shoots bullets at him. The thing is I want him to stop for a moment while doing his fireball attack and change his sprite to "sprite_index = spr_redguyattack".
    Instead when he does the attack he is still running and the running sprite "spr_redguyrun" is displayed. How do I fix my code?

    Code:
    //make enemy run toward player
    
    if (distance_to_point(obj_player.x,y) >= 16)
    {
    if (x<obj_player.x) {
    if (place_free(x+2,y)) {
    x+=1;
    sprite_index = spr_redguyrun;
    image_xscale = -1;
    }
    } else {
    if (place_free(x-2,y)) {
    x-=1;
    sprite_index = spr_redguyrun;
    image_xscale = 1;
                     
     }
     }
     }
     }
     
    //make enemy shoot at player from distance
    
    if (distance_to_point(obj_player.x,y) >= 64)  && !alarm[0] > 0
     {   
    sprite_index = spr_redguyattack;
    att = instance_create(x,y-7,attack);
    att.image_xscale = image_xscale;
    att.speed = 3;
    
    if (image_xscale > 0) {
    att.direction = 180;
    att.image_xscale = 1
    } else {
    att.direction = 0;
    att.image_xscale = -1
    }
     alarm[0] = 50;     
    }       
    }
    }
    

    How do I fix this?
     
  2. KPJ

    KPJ Member

    Joined:
    Jan 19, 2019
    Posts:
    115
    I think the problem could be that both
    Code:
    if (distance_to_point(obj_player.x,y) >= 16)
    and
    Code:
    if (distance_to_point(obj_player.x,y) >= 64)  && !alarm[0] > 0
    are "true". In that case, if this code is in the Step event, the sprite_index will be set to
    Code:
    spr_redguyrun
    each frame. To fix this, you could look into state machines. There are many tutorials on this topic on youtube. I hope this helps!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice