Search code examples
game-maker

How to draw a sprite in Game Maker with gradually increasing alpha (opacity)?


I need to draw a sprite in Game Maker, where the opacity (alpha) of the sprite being drawn is a gradient, and not a fixed value.

In simpler terms, I need the sprite to look like it's fading from one of its edges.


Solution

  • The easy to do this is to apply a gradient to the sprite in the editor under image -> gradient. If you want to do it in code the only way I can think of is:

    for (_t = 0;_t < sprite_get_width(sprite);_t ++){
        draw_sprite_part_ext(sprite,0,_t,0,_t+1,sprite_get_height(sprite),x+_t,y,1,1,c_white,_t/(sprite_get_width(sprite)))
    }
    

    (put this code in the draw event and change "sprite" to your sprite)

    update:

    The above code only works in game maker studio, for game maker version 8.0 or lower change the code to:

    for (_t = 0;_t < sprite_get_width(sprite);_t +=1 ){
        draw_sprite_part_ext(sprite,0,_t,0,1,sprite_get_height(sprite),x+_t,y,1,1,c_white,_t/(sprite_get_width(sprite)))
    }