draw_sprite

Draws a sprite at a given position.

Syntax:

GML Code Example

draw_sprite(sprite, subimg, x, y);


Argument Description
sprite The index of the sprite to draw.
subimg The sub-image (frame) of the sprite to draw (image_index or -1 correlate to the current frame of animation in the object).
x The x coordinate of where to draw the sprite.
y The y coordinate of where to draw the sprite.


Returns: N/A


Description

This function draws the given sprite and sub-image at a position within the game room. For the sprite you can use the instance variable sprite_index to get the current sprite that is assigned to the instance running the code, or you can use any other sprite asset. The same goes for the sub-image, as this can also be set to the instance variable image_index which will set the sub-image to that selected for the current instance sprite (note, that you can draw a different sprite and still use the sub-image value for the current instance), or you can use any other value for this to draw a specific sub-image of the chosen sprite. If the value is larger than the number of sub-images, then GameMaker: Studio will automatically loop the number to select the corresponding image (for example, if the sprite being drawn has 5 sub-images numbered 0 to 4 and we set the index value to 7, then the function will draw sub-image 3, numbered 0). Finally, the x and y position is the position within the room that the sprite will be drawn, and it is centered on the sprite x offset and y offset.


Example:

GML Code Example

draw_sprite(sprite_index, image_index, x, y);
 draw_sprite(spr_Halo, 0, x, y-32);

This will draw the instances assigned sprite (sprite_index) with the current sub-image at the x and y position of the instance within the room. It will then draw the first frame of the sprite indexed by "spr_Halo" at the same x and y position but 32 pixels 'above'.





No Examples Submitted

Does this page need better examples? Be the first to Submit

No Edits Suggested

Is this page unclear or not descriptive enough? Suggest an edit to the page and with enough upvotes your changes will be made.

No Questions Have Been Ask

Do you have a question about this page? Ask it Here