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

Help! Player goes missing ! [SOLVED]

V

Valcia

Guest
this code is in my creation code of my player
< //HP
_hp = 10;

healthbar_width = 285;
healthbar_height = 48;

healthbar_x = 40;
healthbar_y = 0;
//100 pixels above the player />

so i created a draw event, then i made a script like this
if (_hp == 9 )
{
draw_sprite(spr_health_9,0,healthbar_x,healthbar_y)
}
but after using the draw event my player goes missing and the healthbar is not there too idk why.
Please be kind, im quite new to game maker

forgot to mention:
if the player touches the enemy, his hp -= 1;
 
If you put any code inside of an objects draw event, it cancels all automatic drawing. You'll have to put
Code:
draw_self();
In the draw event alongside your other code to get it to draw itself. Also, your healthbar_x and healthbar_y is not set to 100 pixels above the player. It's set simply to an x position of 40 and a y position of 0 in the room. You'll need to constantly update the position and also make it relative to the instances x and y position like this:

Create Event
Code:
healthbar_x = x-40;
healthbar_y = y-100;
Step Event:
Code:
healthbar_x = x-40; // Here we constantly update the position so when the player moves, the healthbar moves as well
healthbar_y = y-100;
You'll probably have to tweak those values to get it where you want it, but notice that it's x plus something and y plus something. That means that where ever the instance is, healthbar_x and healthbar_y will be positioned relative to the x and y of the instance according to the numeric values you put there.
 
V

Valcia

Guest
If you put any code inside of an objects draw event, it cancels all automatic drawing. You'll have to put
Code:
draw_self();
In the draw event alongside your other code to get it to draw itself. Also, your healthbar_x and healthbar_y is not set to 100 pixels above the player. It's set simply to an x position of 40 and a y position of 0 in the room. You'll need to constantly update the position and also make it relative to the instances x and y position like this:

Create Event
Code:
healthbar_x = x-40;
healthbar_y = y-100;
Step Event:
Code:
healthbar_x = x-40; // Here we constantly update the position so when the player moves, the healthbar moves as well
healthbar_y = y-100;
You'll probably have to tweak those values to get it where you want it, but notice that it's x plus something and y plus something. That means that where ever the instance is, healthbar_x and healthbar_y will be positioned relative to the x and y of the instance according to the numeric values you put there.
Bro thanks alot!!!! really helped me out understanding the basics of draw events!! much luv for this reply<3!
 
V

Valcia

Guest
If you put any code inside of an objects draw event, it cancels all automatic drawing. You'll have to put
Code:
draw_self();
In the draw event alongside your other code to get it to draw itself. Also, your healthbar_x and healthbar_y is not set to 100 pixels above the player. It's set simply to an x position of 40 and a y position of 0 in the room. You'll need to constantly update the position and also make it relative to the instances x and y position like this:

Create Event
Code:
healthbar_x = x-40;
healthbar_y = y-100;
Step Event:
Code:
healthbar_x = x-40; // Here we constantly update the position so when the player moves, the healthbar moves as well
healthbar_y = y-100;
You'll probably have to tweak those values to get it where you want it, but notice that it's x plus something and y plus something. That means that where ever the instance is, healthbar_x and healthbar_y will be positioned relative to the x and y of the instance according to the numeric values you put there.
Hey one quick question. Im using health sprites eg: hp_100, hp_90 and so on. I made a code tht if i collide with the enemy, plyer gets knock back. How can i make the sprite_index to hp_90 if get hit by an enemy?
 
Hmmm, having multiple sprites corresponding to particular health values is a non-standard way of doing it, but there's nothing inherently wrong with it. The only thing that I would definitely change is not have separate sprites for each health value, but instead having one sprite with multiple frames (so have a sprite called spr_hp or whatever and have each hp_100, hp_90, etc image as a frame in that sprite). Make sure that your full hp image is the last frame and your 0 hp image is the first frame. It's possible to do it with full hp being the first frame and 0 hp being the last frame, but it'll require some extra maths, so for simplicity sake we'll order it the other way round.

Then we need to figure out what frame corresponds to what health value by using some maths. First, we need to know the max amount of hp you can have and also your current hp, I'll be using the variables hp_current and hp_max to stand in for those values.
Code:
var hp_percent = hp_current/hp_max; // Figure out what percentage your current health is in relation to your max health, this will be 1 if you are at full health (100/100), 0.9 if you lose 10 hp (90/100), etc

var current_frame = round(hp_percent*10); /* Here we convert the decimal 1, 0.9, 0.8, etc, whatever it is, into a rounded whole number. So if you are at full health, it will return 10, if you are at 90 health,
it will return 9, this is the frame that we want to draw from your health sprite that corresponds to the health lost */

draw_sprite(spr_hp,current_frame,healthbar_x,healthbar_y); // Now we draw the health sprite, with whatever image_index (or current_frame as I've called it) we determined it should be
That should work I think, but I haven't tested it. The issue with doing it this way is that you are very much locking yourself into 10 damage point increments. For instance, if you subtract 4 from current_hp at some point, there will be no noticeable change in the healthbar, but whether that's a problem or not totally depends on the design of your game.
 
Top