GMS 2 direction plus sprite change

Discussion in 'Programming' started by Adjud, Jan 15, 2020.

  1. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    73
    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:
    73
    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
  4. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    73
    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,372
    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:
    73
    thank you.... that worked perfectly.... god..... what a stupid error.
     
  7. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    73
    hey,
    so I made sprites that have 6 to 8 frames for attacking, one for left, right and forward, I made a code for the space bar to basically draw the att sprite over top the character sprite but it keeps telling me the sprite thats being drawn does not exist...
    i double checked the spelling but the sprite name is the same as in the code so im not sure whats going on here.

    Create Event for objchar:

    Code:
    satt=0;

    Space Bar event for objchar:

    Code:
    if satt=0 {
       if attdir = 1 {
          draw_sprite(self.x,self.y,1,sprattl)
          sprattl.image_speed=1;
          alarm[3]=6;
          satt=1; 
       }
    }
       
    if satt=0 {
       if attdir = 2 {
          draw_sprite(self.x,self.y,1,sprattr)
          sprattr.image_speed=1;
          alarm[3]=6;
          satt=1;
       }
    }
    
    if satt=0 {
       if attdir = 3 {
          draw_sprite(self.x,self.y,1,sprattf)
          sprattf.image_speed=1;
          alarm[3]=8;
          satt=1;
       }
    }
    
    Alarm[3] for objchar:

    Code:
    satt=0;
    
    [​IMG]

    the screen shot shows the list of sprites I have, objchar draws all of them with code, the walking sprites work the only three that do not are sprattl, sprattr,sprattf.
    I'm not sure why
     

    Attached Files:

    • ss.png
      ss.png
      File size:
      8 KB
      Views:
      0
  8. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,372
    draw_sprite() is sprite first, then image_index, then x, then y .
     
  9. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    73
    Code:
    if satt=0 {
       if attdir = 1 {
          draw_sprite(sprattl,1,self.x,self.y);
          sprattl.image_speed=1;
          alarm[3]=6;
          satt=1; 
       }
    }
       
    if satt=0 {
       if attdir = 2 {
          draw_sprite(sprattr,1,self.x,self.y);
          sprattr.image_speed=1;
          alarm[3]=6;
          satt=1;
       }
    }
    
    if satt=0 {
       if attdir = 3 {
          draw_sprite(sprattf,1,self.x,self.y);
          sprattf.image_speed=1;
          alarm[3]=8;
          satt=1;
       }
    }
    I changed it butnow I'm getting another error code:
    upload_2020-1-18_14-27-38.png
     
  10. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,846
    You are using image_speed like it is the variable of a few sprites. Sprites do not have variables. Instances do. Change the calling instance's variable.
     
  11. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    73
    I removed the image speed line of code, but then my character disapeared... i took out the draw code and the space bar code but my character seems to start out with no sprite and i have no idea why, i didn't do anything with the code to cause this.

    objchar Create Event:

    Code:
    image_index=2;
    image_speed=0;
    motion=0;
    hit=0
    attack=0;
    attdir=0;
    satt=0;
    
    objchar Step Event

    Code:
    image_xscale=2;
    image_yscale=2;
    
    if (keyboard_check(vk_left))  {
        motion=1
        direction = 180;
        speed=3;
        sprite_index=sprstl;
        image_speed=1;
        attdir=1;
    }
    
    if (keyboard_check(vk_right)) {
        motion=1
        direction = 0;
        speed=3;
        sprite_index=sprstr;
        image_speed=1;
        attdir=2;
    }
    
    if (keyboard_check(vk_down)) {
        motion=1
        direction = 270;
        speed=3;
        sprite_index=sprstf;
        image_speed=1;
        attdir=3;
    }
    
    if (keyboard_check(vk_up)) {
        motion=1
        direction = 90;
        speed=3;
        sprite_index=sprstb;
        image_speed=1;
        attdir=4;
    }
    
    if (keyboard_check(vk_nokey)) {
        motion=0;
        speed=0;
        image_speed=0;
        image_index=0;
    }
    
    if hit=1 {
        image_blend=make_colour_hsv(0, 255, random(255));
        alarm[0]=120;
        alarm[1]=10;
        hit=2;
    }
    
    none of the other codes should activate without a collision or click, but the game is starting out with the sprite not there...
     

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