GMS 2 (SOLVED)direction plus sprite change

Discussion in 'Programming' started by Adjud, Jan 15, 2020 at 2:48 AM.

  1. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    59
    Hi,

    I attempted to get my character to move based on the up down left and right keys, while also changing the sprite to the direction of the key pressed based on variables, I have a variable "motion" telling the sprite weather to animate or not. here is the code, the sprite does not change nor does it move, im assuming "friction" isn't being used correctly.

    Code:
    if (keyboard_check_pressed(vk_left))  {
        friction=0;
     motion=1
     dir=2;
     direction = 180;
     speed=1;
    }
    else {
     friction=1;
    }
    
    if (keyboard_check_pressed(vk_right)) {
        friction=0;
     motion=1
     dir=1;
     direction = 0;
     speed=1;
    }
    else {
     friction=1;
    }
    
    if (keyboard_check_pressed(vk_down)) {
        friction=0;
     motion=1
     dir=0;
     direction = 270;
     speed=1;
    }
    else {
     friction=1;
    }
    
    if (keyboard_check_pressed(vk_up)) {
        friction=0;
     motion=1
     dir=3;
     direction = 90;
     speed=1;
    }
    else {
     friction=1;
    }
    
    
    if dir = 0 {
     if motion=1 {
      image_index=sprstf;
      image_speed=1;
     }
     else {
      image_index=sprstf;
      image_speed=0;
     }
    }
    
    if dir = 1 {
     if motion=1 {
      image_index=sprstr;
      image_speed=1;
     }
     else {
      image_index=sprstr;
      image_speed=0;
     }
    }
    
    if dir = 2 {
     if motion=1 {
      image_index=sprstl;
      image_speed=1;
     }
     else {
      image_index=sprstl;
      image_speed=0;
     }
    }
    
    if dir = 3 {
     if motion=1 {
      image_index=sprstb;
      image_speed=1;
     }
     else {
      image_index=sprstb;
      image_speed=0;
     }
    }
     
  2. ctl-f

    ctl-f Member

    Joined:
    Jan 2, 2020
    Posts:
    2
    Well, I'm not 100% sure on the exact result you're aiming for, and by that I mean that I don't know why you're using the friction variable, is it to simply stop when the key is released? Or are you trying to get the character to slide to a stop? As far as your character not moving it makes sense the way you have it set up.
    Code:
    // You've set your code up something like this [psudocode]
    if(key_pressed_up){
        friction = 0;
        ....move stuff
    }
    else {
       friction = 1;
    }
    
    if(key_pressed_down){
        friction = 0;
        ....move stuff
    }
    else {
       friction = 1;
    }
    
    Think about it, if your up_key is pressed the friction is set to 0, but the down key probably isn't pressed so the key_pressed_down's else statement will fire setting the friction to 1 anyway. You've basically locked the character (seeing as you're setting the speed to 1) unless all keys are simultaneously pressed (and even then the speed/direction would cancel out)

    I'm pretty sure that's your problem, I'd suggest a solution if I knew better what you where aiming for. If you're aiming for just basic traditional movement I'd do something more like this:
    Code:
    // again psudocode I'm not claiming this to be the best movement script out there, there's much better ways of doing it, this is just the fastes
    xprevious = x;
    yprevious = y;
    
    x += (key_pressed_right) - (key_pressed_left) * movement_speed;
    y += (key_pressed_down) - (key_pressed_up) * movement_speed;
    
    direction = point_direction(xprevious, yprevious, x, y);
    
    // set sprite based on direction, or delta x/y, etc.
    
    Hope this helps! If you give more details and your goal maybe I can be a bit more help.
    Good Luck
     
  3. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    59
    I tried making it work
    Code:
    xprevious = x;
    yprevious = y;
    
    x += (keyboard_check_pressed(vk_right) - (keyboard_check_pressed(vk_left) * speed;
    y += (keyboard_check_pressed{vk_down) - (keyboard_check_pressed(vk_up) * speed;
    direction = point_direction(xprevious, yprevious, x, y);
    but it has a lot of errors

    I'm just looking for a simple left right up down code, nothing special, eventually i will add in diagonal with two keys pressed, but the biggest test with this is trying to get the sprite to change to walking in the correct direction depending on what way the character is moving hence why i made the variable "dir" 0, 1 2, and 3 represent left right up down, im sure its an archaic way to do it, but its supposed to tell if the character is moving and in the direction it sets the sprite to the walking sprite "l" "r" "f" "b" then sets the image_speed to 1: to allow the gif to play, then when i let go of the key it stops the animation but holds a certain index of that gif.

    if that makes sense?
     
    Last edited: Jan 15, 2020 at 7:43 PM
  4. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    59
    I simplified the code i posted first, merging the two, but it still won't change the sprite to the correct gif, im not sure if im using image_index correctly...

    Code:
    if (keyboard_check(vk_left))  {
        motion=1
        direction = 180;
        speed=1;
        image_index=sprstl
        image_speed=1;
       
    }
    
    
    if (keyboard_check(vk_right)) {
        motion=1
        direction = 0;
        speed=1;
        image_index=sprstr
        image_speed=1;
    }
    
    
    if (keyboard_check(vk_down)) {
        motion=1
        direction = 270;
        speed=1;
        image_index=sprstf
        image_speed=1;
    }
    
    
    if (keyboard_check(vk_up)) {
        motion=1
        direction = 90;
        speed=1;
        image_index=sprstb
        image_speed=1;
    }
    
    if (keyboard_check(vk_nokey)) {
        motion=0;
        speed=0;
        image_index=0;
    }
    
       
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,335
    You are using image index, that refers to the frame of the sprite. You need to use sprite_index.
     
  6. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    59
    thank you.... that worked perfectly.... god..... what a stupid error.
     

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