SOLVED How do I make a hurt indicator pop up on screen?

Ham

Member
I'm new to GMS2 and I'm making a platformer. There are spikes in the game, to tell the player they've been hit, I added a sound effect, but I also want the screen to shine red for a split second when the player touches the spike. I've tried using:

if(place_meeting(x,y,oSpike)) or (place_meeting(x,y,oSmallSpike)){
audio_play_sound(sndHurt, 1, false);
instance_create_layer(0,0,"Spawner",oHurt);
instance_destroy();
alarm[0] = 1 * room_speed;
instance_destroy(oHurt);
instance_create_layer(oSpawner.x,oSpawner.y,"Player",oPlayer);
}

oHurt being a red screen. I also set oHurt's image_alpha to 0.3. But it just doesn't work.
 
Um, why are you destroying the player object and trying to recreate it? (EDIT: I see why, Slyddar is right, you could probably just move the player instead of destroying and recreating) If you want the screen to flash red, a simpler method is to just draw a red rectangle over the screen, something like this:
Create Event:
Code:
red_alpha = 0;
Step Event:
Code:
if (red_alpha > 0) {
   red_alpha -= 0.25; // Changing 0.25 here will effect how fast the red fades, a higher number is a quicker fade
}
else {
   red_alpha = 0;
}
if(place_meeting(x,y,oSpike)) or (place_meeting(x,y,oSmallSpike)){
    audio_play_sound(sndHurt, 1, false);
    x = oSpawner.x;
    y = oSpawner.y;
    if (red_alpha == 0) {
       red_alpha = 1;
   }
}
Draw GUI Event:
Code:
if (red_alpha > 0) {
   var x1 = 0;
   var y1 = 0;
   var x2 = display_get_gui_width();
   var y2 = display_get_gui_height();
   draw_set_alpha(red_alpha);
   draw_rectangle(x1,y1,x2,y2,false);
   draw_set_alpha(1);
}
 

Slyddar

Member
If you want to show it, you need to not destroy it 3 lines after creating it. Also you don't have to destroy the player when they die, you can just set their x/y to your spawner location. You're also setting an alarm[0] of an object you just destroyed the line before, so it will never run.
 
  • Like
Reactions: Ham

Ham

Member
Um, why are you destroying the player object and trying to recreate it? (EDIT: I see why, Slyddar is right, you could probably just move the player instead of destroying and recreating) If you want the screen to flash red, a simpler method is to just draw a red rectangle over the screen, something like this:
Create Event:
Code:
red_alpha = 0;
Step Event:
Code:
if (red_alpha > 0) {
   red_alpha -= 0.25; // Changing 0.25 here will effect how fast the red fades, a higher number is a quicker fade
}
else {
   red_alpha = 0;
}
if(place_meeting(x,y,oSpike)) or (place_meeting(x,y,oSmallSpike)){
    audio_play_sound(sndHurt, 1, false);
    x = oSpawner.x;
    y = oSpawner.y;
    if (red_alpha == 0) {
       red_alpha = 1;
   }
}
Draw GUI Event:
Code:
if (red_alpha > 0) {
   var x1 = 0;
   var y1 = 0;
   var x2 = display_get_gui_width();
   var y2 = display_get_gui_height();
   draw_set_alpha(red_alpha);
   draw_rectangle(x1,y1,x2,y2,false);
   draw_set_alpha(1);
}
Omg, it works now. Thank you so much. And yeah kind of dumb of me to destroy the player and recreate it. Thanks for pointing that out!
 

Ham

Member
If you want to show it, you need to not destroy it 3 lines after creating it. Also you don't have to destroy the player when they die, you can just set their x/y to your spawner location. You're also setting an alarm[0] of an object you just destroyed the line before, so it will never run.
Thanks for showing me! I was so confused why it wasn't showing.
 
Top