Level 06 Lesson 04 [Drawing Sprites in Draw Events]

So far we have drawn the value of variables to the screen using objects that we add to the room (the draw object). We have also changed an object’s sprite using the sprite_index variable. In this lesson we will explore another way of drawing variables and sprites to the screen.

Part A – More About the Draw Event
We have left the draw event blank for most of our game objects. Blank draw events, by default, will draw the object’s sprite for you automatically. Sometimes, however, it is good to code an objects draw event to draw variables and sprites out.   Check out these examples.

Watch the video: 06-04-A
Watch the video: 06-04-B
Done-File: 06-04-A-Done.gmk


Challenge 01 – Draw Potion Life

Open the project 06-04-A-Done.gmk.
Try changing the way that the health potion is drawn out.
Use the draw event of the potion to draw out the sprite for the potion and also the amount of the potion that is left.

Challenge Solution
Solution Video:  06-04-X1-Solution
Solution File: 06-04-X1-Done.gmk

 

Part B – Draw Sprite Extended
Sometimes you need your sprite to be drawn using some extra properties like image_angle, alpha transparency, and scaling. For this you have to use the draw_sprite_ext method. Check out the video

Watch the video: 06-04-C
Done_File: L06-04-C-Done.gmk


Challege 02 – Fading Lasers

Start with the pre-made game project:
L06-04-X2-StartHere.gmk
Watch the Preview Video:
06-04-X2Preview    (sound is messed up, will fix one day…)

You’ll notice that the boss and the laser fire at angles and everything works alright. Here’s what we want to add: Give the lasers an instance variable called strength and set it to 1.0 when they are created. In the step event, make the strength variable decrease by 0.02 (yes, just a little). If the strength variable gets to zero or below, destroy the laser. Test this out.

Now add the draw event to the laser object. Inside of it, use the draw_sprite_ext method to draw the sprite out at the proper angle, scaling, etc.  BUT when it comes time to fill in the alpha parameter, use the strength variable. Since the strength variable is constantly decreasing, the drawn sprite’s alpha will decrease (fade out).

Solution Video: 06-04-X2Solution
Solution File: L06-04-X2-Done.gmk