GML [SOLVED]Sprite Facing Direction

Discussion in 'Programming' started by ejoshuat, Jan 12, 2017.

  1. ejoshuat

    ejoshuat Guest

    I can't find anything to actually help me, I have all the movement sorted out and that but Icant find any code in which will change the player or sprite in the facing direction, so when I move left and right it will face the same direction. HELP

    This is the code for the step event so far its all in working order:

    //Get the player's input
    key_right = keyboard_check(vk_right);
    key_left = -keyboard_check(vk_left);
    key_jump = keyboard_check_pressed(vk_space);

    //React to inputs
    move = key_left + key_right;
    hsp = move * movespeed;
    if (vsp < 10) vsp += grav;

    if (place_meeting(x,y+1,obj_FloorCENT))
    {
    vsp = key_jump * -jumpspeed
    }

    //Horizontal Collision
    if (place_meeting(x+hsp,y,obj_FloorCENT))
    {
    while(!place_meeting(x+sign(hsp),y,obj_FloorCENT))
    {
    x += sign(hsp);
    }
    hsp = 0;
    }
    x += hsp;

    //Vertical Collision
    if (place_meeting(x,y+vsp,obj_FloorCENT))
    {
    while(!place_meeting(x,y+sign(vsp),obj_FloorCENT))
    {
    y += sign(vsp);
    }
    vsp = 0;
    }
    y += vsp;
     
  2. sp202

    sp202 Member

    Joined:
    Sep 26, 2016
    Posts:
    968
    sprite_index is what you're after. Set the sprite_index to left and right facing sprites accordingly when you're moving in either direction.
     
  3. matharoo

    matharoo Udemy Instructor

    Joined:
    Jul 9, 2016
    Posts:
    896
    If you have your player sprite facing right, just use image_xscale to make it face left/right.
    Code:
    if (key_left) image_xscale = -1; else
    if (key_right) image_xscale = 1;
     
  4. ejoshuat

    ejoshuat Guest

    Ill try that one sec
     
  5. ejoshuat

    ejoshuat Guest

    It didnt change the direction that its facing or anything it just stretched the image to be wider thats all that happened?
     
  6. ejoshuat

    ejoshuat Guest

    How do I do that?
     
  7. marsiic

    marsiic Guest

  8. matharoo

    matharoo Udemy Instructor

    Joined:
    Jul 9, 2016
    Posts:
    896
    Well you could be using a different value for the xscale. Anyway, if you need to use sprite_index, you can just use:
    Code:
    if (key_left) sprite_index = <sprite_name>;
    else
    if (key_right) sprite_index = <sprite_name>;
    This changes the sprite (here, sprite_index) to whatever sprite you want when you go in a direction. Change the <sprite_name> with the appropriate sprites and you're good to go.
     
  9. ejoshuat

    ejoshuat Guest

    Still a no doesn't work just disappears.
     
  10. JackTurbo

    JackTurbo Member

    Joined:
    Oct 19, 2016
    Posts:
    823
    I'm not sure why the options posted so far aren't working for you.

    But another option is to use direction to test which way your character is facing.

    Having had another gander at your code I realise now though, its probably easier to do it based off your move variable

    Create event:
    Code:
    //moving right and not jumping o falling
    if (move > 0 && vsp = 0){
        sprite_index = sRightRunSprite; 
        //replace this with what ever your actual sprite is
    }
    
    //moving left and not jumping or falling
    if (move < 0 && vsp = 0){
        sprite_index = sLeftRunSprite; 
        //replace this with what ever your actual sprite is 
    }
    
    //jumping right
    if (move > 0 && vsp > 0){
        sprite_index = sRightJumpSprite; 
        //replace this with what ever your actual sprite is 
    }
    //jumping left
    if (move < 0 && vsp > 0){
        sprite_index = sLeftJumpSprite; 
        //replace this with what ever your actual sprite is 
    }
    
    //Falling right
    if (move > 0 && vsp < 0){
        sprite_index = sRightFallSprite; 
        //replace this with what ever your actual sprite is 
    }
    //Falling left
    if (move < 0 && vsp < 0){
        sprite_index = sLeftFallSprite; 
        //replace this with what ever your actual sprite is 
    }
    
    
    
    
     
    RedSandsProject likes this.
  11. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,302
    image_angle maybe, if game is top-down ?
     
  12. ejoshuat

    ejoshuat Guest

    Thanksss works a dream!
     
  13. ejoshuat

    ejoshuat Guest

    But how do I then get the animation to stop once the key has been released and the player has stopped moving?
     
  14. JackTurbo

    JackTurbo Member

    Joined:
    Oct 19, 2016
    Posts:
    823
    Whoops, sorry I forgot to add in an idle state.

    Stick this at the top of the code I posted earlier.

    Code:
    //Checks if you're not moving vertically or horizontally
    if (move == 0 && vsp == 0){
        //checks if your direction is left
        if (direction >90 && direction < 270){
           sprite_index = sLefttIdleSprite;
        }else{
            sprite_index = sRightIdleSprite;
        }
    
    }
     
  15. ejoshuat

    ejoshuat Guest

    Thankssss man!
     

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