2D AI movement [Solved]

Discussion in 'Programming' started by Choppa, Jan 16, 2017.

  1. Choppa

    Choppa Guest

    Hello everyone! I am new around these parts, but I am currently making a 2D game. The problem that I am facing at the moment is with the AI mechanics. I have no clue how to flip the sprite that is moving.

    Right now as it goes I have a sprite AI that moves left and right. Once it hits a wall it is suppose to turn around and walk the other way. Currently I have the sprite hit a wall and turn to go the other way, but I have no idea how to flip the sprite itself so it looks the other way. Can anyone help me out, or direct me in the path I need to go? Thank you for your time.

    Code:
    ///Initialize Variables
    
    dir = -1; //Horizontal Variable
    //Movement variables
    movespeed = 3;
    grav = 0.35;
    hsp = 0;
    vsp = 0;
    Code:
    hsp = dir * movespeed;
    vsp += grav;
    
    //Horizontal Collision
    if (place_meeting(x+hsp,y,obj_grassblock1))
    {
        while(!place_meeting(x+sign(hsp),y,obj_grassblock1))
        {
            x += sign(hsp);
        }
        hsp = 0;
        dir *= -1;
    }
    if (place_meeting(x+hsp,y,obj_grassblock2))
    {
        while(!place_meeting(x+sign(hsp),y,obj_grassblock2))
        {
            x += sign(hsp);
        }
        hsp = 0;
       
        dir *= -1;
    }
    if (place_meeting(x+hsp,y,obj_aibar))
    {
        while(!place_meeting(x+sign(hsp),y,obj_aibar))
        {
            x += sign(hsp);
        }
        hsp = 0;
        dir *= -1;
    }
    x += hsp;
    //Vertical Collision
    if (place_meeting(x,y+vsp,obj_grassblock1))
    {
        while(!place_meeting(x,y+vsp,obj_grassblock1))
        {
            y += sign(vsp);
        }
        vsp = 0;
    }
    if (place_meeting(x,y+vsp,obj_grassblock2))
    {
        while(!place_meeting(x,y+vsp,obj_grassblock2))
        {
            y += sign(vsp);
        }
        vsp = 0;
        dir *= -1;
    }
    y += vsp;
    
     
  2. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    626
    If I remember correctly: image_xscale set to -1 reverses the image.

    So you just need to do something to link that to the direction it's heading.

    If your sprite was drawn facing right, then when it's heading right image_xscale =1.

    If it's heading left then image_xscale = -1

    (but my memory of this is a bit fuzzy.....)
     
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,876
    image_xscale = dir;
     
  4. Choppa

    Choppa Guest

    Thanks!
     

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