SOLVED? Getting stuck in Wall

Discussion in 'Programming' started by Velocity, Sep 1, 2016.

  1. Velocity

    Velocity Member

    Joined:
    Aug 28, 2016
    Posts:
    148
    Hey,

    So I'm making a fighting game and my character is getting stuck in the walls.

    I set my player object mask as a sprite, so that the sprites wouldn't change and stick me into the wall.

    Also, I made sure that sprite had an odd number of pixels and set the origin to the exact pixel horizontally in the middle of the sprite.

    Plus, I also used Shaun Spalding's video tutorial about platformer basics, and set a collision checker preventative measure against getting stuck in the walls.

    I even used another solid object (placed over the wall object) and used a normal collision event between that and the player object.

    I'm wondering if it's because Game Maker isn't recognising a variable I made global, called "global.playerspeed" instead of hsp.

    By the way, I'm making my game with someone else's code. He also had a couple of problems with getting stuck in the walls, but only the opponent used to get stuck when he moved back into the wall after getting hit. I also changed his sprites to my new ones.

    So, here's my code:

    //Horizontal collision to avoid getting stuck in wall
    if (place_meeting(x + global.playerspeed, y, obj_player_parent))
    {
    while (!place_meeting(x+sign(global.playerspeed),y,obj_player_parent))
    {
    x += sign (global.playerspeed) ;
    }
    global.playerspeed = 0;
    }
     
  2. Velocity

    Velocity Member

    Joined:
    Aug 28, 2016
    Posts:
    148
    Whoops, I accidentally replied to my own post and don't know how to delete it.
     
  3. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,450
    The most common cause of characters getting stuck in platformer walls is using animated sprites without a static collision mask.

    Make a solid-fill, one-frame rectangular sprite with the same size and origin as your character, then go into Object Properties and set Mask to it.
     
    mastema likes this.
  4. Velocity

    Velocity Member

    Joined:
    Aug 28, 2016
    Posts:
    148
    Thanks for the comment. Yeah, I did that already. Except, I don't think there is an "origin for my character", because my character object is a parent with no sprite attached to it.
     
  5. Gre_Lor12

    Gre_Lor12 Guest

    Well at this point you can do 2 simple things (with various others):

    1. override the parent by putting in the "create event" the "Inherit the parent event" block and then create a small script with: sprite_index = [sprite_name] that will then set an origin ;)
    2. Create a collision box using collision_rectangle(x1, y1, x2, y2, prec, notMe):
    var coll = [object]
    //Horizontal collision to avoid getting stuck in wall
    if (collision_rectangle(x + global.playerspeed, y, x, y, false, true) )
    if (place_meeting(x + global.playerspeed, y, obj_player_parent) )
    {
    while (!place_meeting(x+sign(global.playerspeed),y,obj_player_parent))
    {
    x += sign (global.playerspeed) ;
    }
    global.playerspeed = 0;
    }


    This is only a rough kinda script so I am not sure if it will work in that format so you may have to throw it about a bit. ;/
     
    Last edited by a moderator: Sep 1, 2016
  6. Gre_Lor12

    Gre_Lor12 Guest

    Either that or take out the global.playerSpeed = 0; and change the while a little to add an else in to set the playerspeed to 0 when needed
     
  7. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,556
    Do all the children use the mask as well, or just the parent?
     
    Gre_Lor12 likes this.
  8. Velocity

    Velocity Member

    Joined:
    Aug 28, 2016
    Posts:
    148
    At first I applied the mask to the main child that I'm working on (my only character that's being fleshed out first), then I applied it to the parent. I tried both.
     
  9. Velocity

    Velocity Member

    Joined:
    Aug 28, 2016
    Posts:
    148
    Okay, so where do I put the collision box code? I tried putting that in the create event for the parent as well and got a red error. I'm pretty new to Game Maker by the way.
     
  10. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,556
    What's the error message? Copy the text and paste it here.
     
  11. Velocity

    Velocity Member

    Joined:
    Aug 28, 2016
    Posts:
    148
    With the "getting stuck in walls" problem, there was no error message - I just got stuck in the walls and had to escape the game.
     
  12. Velocity

    Velocity Member

    Joined:
    Aug 28, 2016
    Posts:
    148
    Okay, so I kind of solved this, but not really, but kinda did.

    I simply remade the game again, and the problem didn't happen anymore.

    I didn't really figure out how to fix the problem, but rather just found a way around it.

    Should I mark this thread as solved? :confused:
     
    Gre_Lor12 likes this.
  13. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,556
    I was referring to what you called a "red error"...
     
  14. Gre_Lor12

    Gre_Lor12 Guest

    Nah place it in the Step Event.

    The Create Event only runs things once aka - when the object is created.

    The Step event runs it every step. Now the number of steps is equal to your room speed. And your room speed is also your max FPS.

    So if you have a room speed of 30, that means your max FPS is 30 and also there are 30 steps in a second
     
    Velocity likes this.
  15. Gre_Lor12

    Gre_Lor12 Guest

    The create event can be used for "ifs" but it will only check it one. If you want something always checking, put it in the step event.

    Begin Step and End Step are there too, but I doubt that you need them in this case
     
  16. Gre_Lor12

    Gre_Lor12 Guest

    Sorry I never realized this said Solved... :/
     
  17. Velocity

    Velocity Member

    Joined:
    Aug 28, 2016
    Posts:
    148
    All good
     
    Gre_Lor12 likes this.
  18. Velocity

    Velocity Member

    Joined:
    Aug 28, 2016
    Posts:
    148
    I think I just got that red in the column to the left of the code when I presumably put it into a create event. It's been a while since I did it.
     
    Gre_Lor12 likes this.
  19. cboyd224

    cboyd224 Member

    Joined:
    Feb 3, 2018
    Posts:
    7
    just wondering if someone can give me an answer that works with the drag and drop method on game maker studio 2
     
  20. cboyd224

    cboyd224 Member

    Joined:
    Feb 3, 2018
    Posts:
    7
    Hey everyone im trying to learn gms2 and i was wondering if someone can reply with and answer to this question using the drag and drop option to prevent a player from getting stuck on a wall
     
  21. cboyd224

    cboyd224 Member

    Joined:
    Feb 3, 2018
    Posts:
    7
    for anyone thats using the drag and drop version, this is a way i figured out with getting stuck on a wall if you want to know

    alright i figured it out, i had to take what i was watching on a video on youtube and try to apply it to the drag and drop version, i was confused only because the drag and drop version doesn't explain the variable and actions very well, like throwing in the action "key down right" for explain is like a if variable for that key,

    so in that action of "key down right"
    - i put in the "if expression" (place_free(x + colspd, y) )
    //in "create" i had (var colspd = walkspd +2) where just so you know var walkspeed = 4 and colspd means collisionspeed//

    -followed by an "if variable" (x greater or equal walkspd) then followed by
    -"jump to point" x=walkspd "relative" y=_
    -with the set sprite after it for the player to look in that direction
     

    Attached Files:

  22. cboyd224

    cboyd224 Member

    Joined:
    Feb 3, 2018
    Posts:
    7
    so yeah im super happy lol
     
  23. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,556
    Just FYI, drag-and-drop is much more cumbersome to share than code (as you probably noticed when you had to type everything out - with code, you'd just needed Ctrl-A, Ctrl-C, Ctrl-V and boom, post done) so nobody that shares code on a regular basis will give you DnD... it just takes so much longer :p

    (Not to mention code has a bunch of other benefits, including being faster to create - you don't need to drag it around, and you can type out any command you're aware exists without having to find it in a list first)
     
  24. magnum00125

    magnum00125 Member

    Joined:
    Feb 20, 2018
    Posts:
    86
    It's possible to share the code of a drag and drop event by converting it to GML but once it's GML u can't convert it back to D&D so I would recommend you make a new event you don't need with same drag and drop "code" and convert that to GML to share. lol. an "expendable" event if you will
     

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