optimizing movement

Discussion in 'Programming' started by supermasher/ti, Jan 10, 2019.

  1. supermasher/ti

    supermasher/ti Member

    Joined:
    Jan 3, 2019
    Posts:
    28
    is there any way i can optimize this movement code to save space

    OBJ_Player

    Crate//:
    Code:
    idle = 1;
    walking = 0;
    
    up = 3;
    right = 2;
    down = 1;
    left = 0;
    
    Action = idle;
    Direction = down;
    
    View[1,3] = v_p_upIdle;
    View[1,2] = v_p_rightIdle;
    View[1,1] = v_p_downIdle;
    View[1,0] = v_p_leftIdle;
    
    View[0,3] = v_p_upWalking;
    View[0,2] = v_p_rightWalking;
    View[0,1] = v_p_downWalking;
    View[0,0] = v_p_leftWalking;
    
    Step//:
    Code:
    /// @description
     
    if (keyboard_check(vk_up) || keyboard_check(vk_right) || keyboard_check(vk_down) || keyboard_check(vk_left)) {
        Action = walking;
        if (keyboard_check(vk_up)) {Direction = up;}
        if (keyboard_check(vk_right)) {Direction = right;}
        if (keyboard_check(vk_down)) {Direction = down;}
        if (keyboard_check(vk_left)) {Direction = left;}
    } else {Action = idle}
    
    sprite_index = View[Action, Direction];
    
    
    speed = 0;  //this doesn't work if you have acceleration, but you don't have acceleration probably
    if (keyboard_check(vk_up) || keyboard_check(vk_right) || keyboard_check(vk_down) || keyboard_check(vk_left)) {
       Action = walking;
       if (keyboard_check(vk_up)) {Direction = up; vspeed = -2;}
       if (keyboard_check(vk_right)) {Direction = right; hspeed = 2;}
       if (keyboard_check(vk_down)) {Direction = down; vspeed = 2;}
       if (keyboard_check(vk_left)) {Direction = left; hspeed = -2;}
    } else {Action = idle;}
    
    
    
    here is a video of what I have right now

    https://photos.app.goo.gl/8tfhcUpk4cZaizSZ8

    and i want it to look like what i have in the video
     
  2. 2Dcube

    2Dcube Member

    Joined:
    Jun 23, 2016
    Posts:
    294
    create:
    Code:
    key[0] = vk_right;
    key[1] = vk_up;
    key[2] = vk_left;
    key[3] = vk_down;
    
    idle = 1;
    walking = 0;
    Action = idle;
    
    Direction = 3; // down (3 * 90 = 270)
    
    View[1,0] = v_p_rightIdle;
    View[1,1] = v_p_upIdle;
    View[1,2] = v_p_leftIdle;
    View[1,3] = v_p_downIdle;
    
    View[0,0] = v_p_rightWalking;
    View[0,1] = v_p_upWalking;
    View[0,2] = v_p_leftWalking;
    View[0,3] = v_p_downWalking;
    
    step:
    Code:
    speed = 0;
    Action = idle;
    sprite_index = View[Action, Direction];
    
    for(var i=0; i<4; i++)
    {
      if keyboard_check(key[i])
      {
        hspeed = lengthdir_x(2,i * 90);
        vspeed = lengthdir_y(2,i * 90);
    
        Action = walking;
        Direction = i;
        sprite_index = View[Action, Direction];
      }
    }
    Something like this? I simplified all the keyboard checks you did.
     
    Last edited: Jan 10, 2019
  3. Sam04

    Sam04 Member

    Joined:
    Nov 26, 2018
    Posts:
    24
    I would recommend using enumerators and you dont really need the first check on all the keys if you are doing the same thing in the second call. It would be something like this:

    Create:
    Code:
    enum actionList {
        walking,
        idle
    }
    
    enum directions {
        right,
        up,
        left,
        down
    }
    
    currentAction = actionList.idle;
    currentDirection = directions.down;
    hkey = 0;
    vkey = 0;
    
    View[1,3] = v_p_downIdle;
    View[1,2] = v_p_leftIdle;
    View[1,1] = v_p_upIdle;
    View[1,0] = v_p_rightIdle;
    
    View[0,3] = v_p_downWalking;
    View[0,2] = v_p_leftWalking;
    View[0,1] = v_p_upWalking;
    View[0,0] = v_p_rightWalking;
    
    I changed the sprites order in the array to match the angles I'm going to use.

    Step:
    Code:
    hkey = keyboard_check(vk_right) - keyboard_check(vk_left);
    vkey = keyboard_check(vk_down) - keyboard_check(vk_up);
    
    if (hkey == 0 && vkey == 0) {
        currentAction = actionList.idle;
        speed = 0;
    } else {
        currentAction = actionList.walking
        currentDirection = point_direction(0,0, hkey,vkey) div 90;
        hspeed = sign(lengthdir_x(1,currentDirection*90))*2;
        vspeed = sign(lengthdir_y(1,currentDirection*90))*2;
    }
    sprite_index = View[currentAction, currentDirection];
    
     
    2Dcube likes this.
  4. supermasher/ti

    supermasher/ti Member

    Joined:
    Jan 3, 2019
    Posts:
    28

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