• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

stick an Object to another object

T

tomast

Guest
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!
 

Alexx

Member
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:
T

Tiilerdye

Guest
Why not just draw the life bar in the space ships draw event? Should fix your problem right away
 
T

tomast

Guest
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;
Thanks that works just fine.


Why not just draw the life bar in the space ships draw event? Should fix your problem right away
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!!
 
Top