i am confused on how to make walls

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

  1. supermasher/ti

    supermasher/ti Member

    Joined:
    Jan 3, 2019
    Posts:
    35
    i did tried watching a few videos but i got confused or they were not working properly so i am wondering how to add walls Slide1.PNG so all i have right now is movement

    here is my code so far

    OBJ_player

    Creat//:
    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;}
    
    //walls
    
    and i did try something like this to add walls but it did not work and it started glitching

    Step//:
    Code:
    //walls
    
    if(place_meeting(x+hspeed,y,OBJ_Colishion))
    {
        while(place_meeting(x+sign(hspeed),y,OBJ_Colishion))
        {
            x= x+sign(hspeed);
        }
        hspeed = 0;
    }
    
     
  2. supermasher/ti

    supermasher/ti Member

    Joined:
    Jan 3, 2019
    Posts:
    35
    i am using GMS2
     
  3. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    852
    You need to only do the while loop if there is not a place_meeting, represented by !
     
  4. The Video Gamester

    The Video Gamester Member

    Joined:
    Dec 2, 2016
    Posts:
    62
    if(!place_meeting(x+hspeed,y,OBJ_Colishion))
    {
    while(!place_meeting(x+sign(hspeed),y,OBJ_Colishion))
    {
    x= x+sign(hspeed);
    }
    hspeed = 0;
    }

    I think that should fix it... If I am understanding the only issue at least
     
  5. supermasher/ti

    supermasher/ti Member

    Joined:
    Jan 3, 2019
    Posts:
    35
    the video gamester

    for some reason when i use you code the play game window won't show up but if I remove you code it comes up fine
     
  6. OblivionSkull21

    OblivionSkull21 Member

    Joined:
    Sep 26, 2017
    Posts:
    255
    Check the compiler window for any errors. If it says "non-zero status" or something like that, you've got an error... scroll through the compiler until you find what's wrong.

    As for walls, are you wanting the player to collide with walls? Then you just make a collision event with the obj_wall.
     
  7. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    852
    That's because it's a endless loop.

    You are saying if there is not a collision, then run this collision code that loops while there is not a collision.

    I've given the correct code above.
     
    Last edited: Jan 11, 2019
  8. Warspite2

    Warspite2 Member

    Joined:
    Jun 23, 2016
    Posts:
    93
    Why not simply make a wall object then mark it as solid, then just put in a collision event?
     
  9. The Video Gamester

    The Video Gamester Member

    Joined:
    Dec 2, 2016
    Posts:
    62
    I don't think its an infinite loop. I use similar code for my movement
    Code:
    if(checkGrid(x+hspd,y)){
        while(!checkGrid(x+sign(hspd),y)){
            x+= sign(hspd);
        }
        hspd = 0;
        if(destroy){
            instance_destroy();
        }
    }
    my check grid function acts exactly as the gamemaker check position does. It never causes me any issues. The while loop is so that you get pixel perfect collisions. Edit: My bad. I accidently added an extra !.
     
    Last edited: Jan 11, 2019
  10. The Video Gamester

    The Video Gamester Member

    Joined:
    Dec 2, 2016
    Posts:
    62
    Any error messages in the output window?

    I did actually throw in an extra ! symbol. corrected code

    Code:
    if(place_meeting(x+hspeed,y,OBJ_Colishion))
    {
    while(!place_meeting(x+sign(hspeed),y,OBJ_Colishion))
    {
    x= x+sign(hspeed);
    }
    hspeed = 0;
    }
     
  11. The Video Gamester

    The Video Gamester Member

    Joined:
    Dec 2, 2016
    Posts:
    62
    I try to avoid making objects solid. Its simpler for collisions but its inflexible. It is also possibly much more resource heavy as most of the gamemaker functions are... Not sure on that one.
     
    RefresherTowel likes this.
  12. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,101
    Yeah, solid, along with a few other in-built GM variables should really be avoided. They remove control in ways that can introduce impossible to fix bugs (impossible to fix because GM itself is handling the bit that needs fixing).
     
  13. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    852
    haha, all good. One symbol difference breaks the whole code.
     
  14. The Video Gamester

    The Video Gamester Member

    Joined:
    Dec 2, 2016
    Posts:
    62
    Tell me about it.
     

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