GMS 2 (SOLVED) changing x position not working

Discussion in 'Programming' started by Shadowblitz16, Sep 20, 2017.

  1. Shadowblitz16

    Shadowblitz16 Member

    Joined:
    Jun 23, 2016
    Posts:
    596
    does anybody know why I can't offset my player when I flip him?
    he flips he just doesn't change position.

    here is my code
    Code:
    /// @description Link Step Event
    // You can write your code in this editor
    
    x = px;
    y = py;
    
    //Update Link Here.
    //Horizontal Collision
    
    hitbox_top    = bbox_top    //y - sprite_get_yoffset(sprite_index)// sprite_get_bbox_top(sprite_index)    - sprite_get_yoffset(sprite_index);
    hitbox_left   = bbox_left   //x - sprite_get_xoffset(sprite_index)// sprite_get_bbox_left(sprite_index)   - sprite_get_xoffset(sprite_index);
    hitbox_right  = bbox_right+1  //x + sprite_get_xoffset(sprite_index)// sprite_get_bbox_right(sprite_index)  - sprite_get_xoffset(sprite_index);
    hitbox_bottom = bbox_bottom+1
    hitbox_middle = hitbox_left + (hitbox_right  - hitbox_left) / 2;
    hitbox_center = hitbox_top  + (hitbox_bottom - hitbox_top)  / 2;
    
    if (debug) state = LS_DEBUG;
    
    
    switch(state)
    {
        case LS_DEBUG: link_debug();
        case LS_IDLE: link_state_idle(); break;
        case LS_WALK: link_state_walk(); break;
        case LS_JUMP: link_state_jump(); break 
    
    }
    
    //if (!instance_exists(Level)) return;
    //perform_tile_collision(Level, x, y, hsp, vsp);
    x += xspd;
    y += yspd;
    px = x;
    py = y
    x = floor(x);
    y = floor(y);
    
    Code:
    /// @description link_state_idle()
    
    
    
    if (Input.a)
    {
        state = LS_JUMP;
        sprite_index =  spr_link_duck;
        image_index  =  1;
        image_speed  =  1;
    }
    if (Input.left)
    {
        state = LS_WALK;
        sprite_index =  spr_link_walk;
        image_index  =  1;
        image_speed  =  1;
        flip(-1)
    }
    if (Input.right)
    {
        state = LS_WALK;
        sprite_index =  spr_link_walk;
        image_index  =  1;
        image_speed  =  1;
        flip( 1);
    }
    
    if (instance_exists(Level))
    {
        yspd += G_GRAV * (grid_collision_meeting(Level, x, y, 3)) ? grav1_mod : grav2_mod;
        if(!noclip) perform_tile_collision(Level, id);
    }
    
    Code:
    /// @desc flip()
    /// @param sign
    
    var signval = argument[0];
    
    if (image_xscale > 0 && signval == -1)
    {
        image_xscale =  -1;
        x += sprite_get_width(sprite_index) - sprite_get_xoffset(sprite_index);
    }
    else if (image_xscale < 0 && signval ==  1)
    {
        image_xscale =   1;
        x -= sprite_get_width(sprite_index) - sprite_get_xoffset(sprite_index);
    }
    
    show_debug_message("FLIP: "+string(image_xscale));
    
    Code:
    /// @desc perform_tile_collision()
    /// @arg level
    /// @arg object
    
    //Arguments
    var level  = argument[0];
    var object = argument[1];
    
    
    //Reset collision flags, We are about to check for collision again
    object.collision_top = false;
    object.collision_left = false;
    object.collision_right = false;
    object.collision_bottom = false;
    
    //Check horizontal collision
    if (grid_collision_meeting(level, object.x+object.xspd, object.y, 1))
    {
        while (!grid_collision_meeting(level, object.x+object.xspd, object.y, 1))
        {
            object.x += sign(object.xspd);
        }
     
     
        if (object.xspd >= 0) object.collision_right = true;
        else                  object.collision_left  = true;
     
        object.xspd = 0;
    }
    
    //Check vertical collision
    if (grid_collision_meeting(level, object.x, object.y+object.yspd, 1))
    {
        while (!grid_collision_meeting(level, object.x, object.y+object.yspd, 1))
        {
            object.y += sign(object.yspd);
        }
     
        if (object.yspd >= 0) object.collision_bottom = true;
        else                  object.collision_top    = true;
     
        object.yspd = 0;
    }
    
    Code:
    ///@desc grid_collision_meeting()
    ///@arg level
    ///@arg x
    ///@arg y
    ///@arg type
    
    var level = argument[0];
    var xx    = argument[1];
    var yy    = argument[2];
    var type  = argument[3];
    
    //Set return to false initaly
    value = false;
    
    // Remeber out position
    var xp = x;
    var yp = y;
    
    // Update the position for the bbox calculations
    x = xx;
    y = yy;
    
    //Check vertical points
    for (var top=bbox_top;  top<=bbox_bottom+1; top+=GRID_HEIGHT)
    {
        var l_meeting = (level.grid[# (bbox_left +0) div GRID_WIDTH, (top) div GRID_HEIGHT] == type)
        var r_meeting = (level.grid[# (bbox_right+1) div GRID_WIDTH, (top) div GRID_HEIGHT] == type)
        if (l_meeting || r_meeting) { value = true; break; }
    }
    
    //Check horizontal points
    for (var left=bbox_left; left<=bbox_right+1;  left+=GRID_WIDTH)
    {
        var t_meeting = (level.grid[# (left) div GRID_WIDTH, (bbox_top   +0) div GRID_HEIGHT] == type)
        var b_meeting = (level.grid[# (left) div GRID_WIDTH, (bbox_bottom+1) div GRID_HEIGHT] == type)
        if (t_meeting || b_meeting) { value = true; break; }
    }
    
    x = px;
    y = py;
    
    //show_debug_message("COLLISION: "+string(value))
    return value;
    
    it acts like I am resetting my x somewhere but I'm not.. i don't think.
     
    Last edited: Sep 20, 2017
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,174
    damn your code is complicated,
    wouldn't it just work if you switch the image_xscale and have the x origin of the object slightly to the left of the middle?
     
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,889
    yeah, why isn't your Sprite origin horizontally centered? what reason do you have not to center it? It would make things much easier if you did.

    but aside from that, your flip script looks to me like it should work, so I would think that there's an issue somewhere else other than inside that script which is causing the instance to not move. you don't have physics enabled, do you?
     
    Last edited: Sep 20, 2017
  4. SnotWaffle Studios

    SnotWaffle Studios Member

    Joined:
    Jun 21, 2016
    Posts:
    286
    In the grid_collision_meeting() script, shouldn't it be this at the end:
    Code:
    x = xp;
    y = yp;
    
    instead of
    Code:
    x = px;
    y = py;
    
    Edit: It's probably unrelated to the problem though.
     
  5. Shadowblitz16

    Shadowblitz16 Member

    Joined:
    Jun 23, 2016
    Posts:
    596
    @Simon Gust ya I'm trying to make it as modular as possable

    @TheouAegis it would still have to be offsetted by 1 pixel I beleave since the sprite has a even width
    but would like to make it work with all offsets

    @SnotWaffle Studios px stands for "partial x". and xp stands for "x previous" so your right that is the problem thankyou.
     
    SnotWaffle Studios likes this.
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,889
    If your sprite is 32x32, the center is (16,16), which is perfectly centered for image_xscale and image_yscale flipping. You only need to add 1 if the sprite us odd width.

    and while making it work with all offsets is admirable, it's not necessarily correct. If you have a sprite that is of a guy (24x32) holding a gun (16x8) such that the entire sprite is 40x32 and the origin is relative to the guy at (12,32), you wouldn't want to offset the position after flipping logically, since the guy would be pivoting around his origin, thus whipping the gun around.


    But whatever. Like I said, your flip code looked fine.
     
    Last edited: Sep 20, 2017

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