1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

stick an Object to another object

Discussion in 'Programming' started by tomast, Aug 12, 2019.

  1. tomast

    tomast Member

    Joined:
    Aug 12, 2019
    Posts:
    2
    Hi everyone, I have a small issue with my what i am working with. I am making a game were you control a spaceship and that spaceship has a Lifebar bellow it.

    The spaceship only goes up, and it has set a gravity that sends the ship back to the bottom when you are not pressing the up button.

    The problem is that when the spaceship goes up the lifebar follows it perfectly with the correct distance between both objects. the problem is that when you stop pressing the up button the ship goes down a little bit faster than the lifebar so the distance between both is smaller (even the lifebar gets over the ship) until it reach de lowest point or you press the up button (where both ship and lifebar gets in the correct position).

    In other words, there is a slight delay between the ship and the lifebar when the ship goes down because of the gravity.

    My code is very simple
    on the create event of the ship
    Code:
    grv = 0.5;
    on the step event on the ship:

    Code:
    up = keyboard_check(ord('W'));
    
    oLifebar.x = -179 + self.x;
    oLifebar.y = 70+self.y;
    
    vsp = vsp + grv;
    if (up){
    vsp = -4;
    }
    //Vertical Collision
    var emptyspace= 0;
    if (place_meeting(x,y+vsp,Owall)){
        emptyspace= false;
    } else{
    emptyspace= true
    }
    
    //movement
    if (emptyspace== false)
    {  
        vsp = 0
    }
    y = y + vsp;
    
    
    What I want is to perfectly stick it as if it was the same object

    Thanks in advance, any help would be greatly valued!
     
  2. Alexx

    Alexx Member

    Joined:
    Jun 21, 2016
    Posts:
    441
    You're updating your y value after you try and stick the oLifebar to it.

    Try putting the following in an End Step Event:

    Code:
    oLifebar.x = -179 + self.x;
    oLifebar.y = 70+self.y;
     
    Last edited: Aug 12, 2019
    tomast likes this.
  3. Tiilerdye

    Tiilerdye Member

    Joined:
    Jan 2, 2017
    Posts:
    21
    Why not just draw the life bar in the space ships draw event? Should fix your problem right away
     
    tomast likes this.
  4. tomast

    tomast Member

    Joined:
    Aug 12, 2019
    Posts:
    2
    Thanks that works just fine.


    That was my next option if I could not make it work this way. Maybe it should have been the first thing to try.


    Thanks to both of you for your quick reply!!
     

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