Game Dev Tutorials

How To Make A Sprite Flash White When Hit In GameMaker


When you want to create that white damage flash or blink effect when your shoot at enemies in your game… you have two options.

The first is by creating a white-filled sprite for every sprite that you want this effect to apply on. But that can be a painful process especially if you are objects have animated sprites.

The second and more efficient way, is to use shaders.

In your GameMaker project, create a shader and name it shd_white_sprite (or any other name).

Next, go to the Fragment shader by clicking on the Fragment tab.

In the main() function, comment that entire line and just below it add the following line of code so that the whole script looks like this.

//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
    //gl_FragColor = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
    gl_FragColor = vec4(v_vColour.rgb, texture2D( gm_BaseTexture, v_vTexcoord ).a);
}

That’s it.

Now, in the DRAW EVENT of the object you wish to have this effect work on, simply add the following code.

shader_set(shd_white_sprite);
draw_self();
shader_reset();

However, this will make the sprite white constantly. To make it flash, just play around with IF STATEMENTS based on what you are going for.

The code below will make the object flash white when the spacebar is held down but return back to normal when it is not.

if(keyboard_check(vk_space)){
    shader_set(shd_white_sprite);
    draw_self();
    shader_reset();
}else{
    draw_self();
}

Now. I am no guru with shaders. I have no idea how to change the color to like red. If you know how, please let me know in the comments below.

Hope this helps.

Liked this post?

Enter you email address below and I will send the latest blog updates straight to your inbox. Only when it's relevant. You won't have to miss a post.


Tutorials you might also like