Chapter 32 GML: Game graphics
32.2 Sprites and images
Each object has a sprite associated with it. This is either a single image or it consists of multiple image. For each instance of the object the program draws the corresponding image on the screen, with its origin (as defined in the sprite properties) at the position (x,y) of the instance. When there are multiple images, it cycles through the images to get an animation effect. There are a number of variables that affect the way the image is drawn. These can be used to change the effects. Each instance has the following variables:
visible If visible is true (1) the image is drawn, otherwise it is not drawn. Invisible instances are still active and create collision events; you only don’t see them. Setting the visibility to false is useful for e.g. controller objects (make them non-solid to avoid collision events) or hidden switches.
sprite_index This is the index of the current sprite for the instance. You can change it to give the instance a different sprite. As value you can use the names of the different sprites you defined. Changing the sprite does not change the index of the currently visible sub- image.
sprite_width* Indicates the width of the sprite. This value cannot be changed but you might want to use it.
sprite_height* Indicates the height of the sprite. This value cannot be changed but you might want to use it.
sprite_xoffset* Indicates the horizontal offset of the sprite as defined in the sprite properties. This value cannot be changed but you might want to use it.
sprite_yoffset* Indicates the vertical offset of the sprite as defined in the sprite properties. This value cannot be changed but you might want to use it.
image_number* The number of sub- images for the current sprite for the instance (cannot be changed).
image_index When the image has multiple sub-images the program cycles through them. This variable indicates the currently drawn sub- image (they are numbered starting from 0). You can change the current image by changing this variable. The program will continue cycling, starting at this new index.
image_single Sometimes you want a particular sub-image to be visible and don't want the program to cycle through all of them. This can be achieved by setting this variable to the index of the sub- image you want to see (first sub- image has index 0). Give it a value –1 to cycle through the sub-images. This is useful when an object has multiple appearances. For example, assume you have an object that can rotate and you create a sprite that has sub-images for a number of orientations (counter-clockwise). Then, in the step event of the object you can set
{
image_single = direction * image_number/360; }
image_speed The speed with which we cycle through the sub- images. A value of 1 indicates tha t each step we get the next image. Smaller values will switch sub- images slower, drawing each sub- image multiple times. Larger values will skip sub- images to make the motion faster.
depth Normally images are drawn in the order in which the instances are created. You can change this by setting the image depth. The default value is 0, unless you set it to a different value in the object properties. The higher the value the further the instance is away. (You can also use negative values.) Instances with highe r depth will lie behind instances with a lower depth. Setting the depth will guarantee that the instances are drawn in the order you want (e.g. the plane in front of the cloud). Background instances should have a high (positive) depth, and foreground instances should have a low (negative) depth.
image_scale A scale factor to make larger or smaller images. A value of 1 indicates the normal size. Changing the scale also changes the values for the image width and height and influences collision events as you might expect. Realize that scaled images (in particular when you make them smaller) take more time to draw. Changing the scale can be used to get a 3-D effect.
image_alpha Transparency (alpha) value to use when drawing the image. A value of 1 is the normal setting; a value of 0 is completely transparent. Use with care. Drawing partially transparent images takes a lot of time and will slow down the game.
bbox_left* Left side of the bounding box used of the image of the instance (taking scaling into account).
bbox_right* Right side of the bounding box of the instance image
bbox_top* Top side of the bounding box of the instance image.
bbox_bottom* Bottom side of the bounding box of the instance image.
32.3 Backgrounds
Each room can have up to 8 backgrounds. Also it has a background color. All aspects of these backgrounds you can change in a piece of code using the following variables (note that some are arrays that range from 0 to 7, indicating the different backgrounds):
background_color Background color for the room.
background_showcolor Whether to clear the window in the background color.
background_visible[0..7] Whether the particular background image is visible.
background_foreground[0..7] Whether the background is actually a foreground.
background_index[0..7] Background image index for the background.
background_x[0..7] X position of the background image.
background_y[0…7] Y position of the background image.
background_width[0…7]* Width of the background image.
background_htiled[0..7] Whether horizontally tiled.
background_vtiled[0..7] Whether vertically tiled.
background_hspeed[0..7] Horizontal scrolling speed of the background (pixels per step).
background_vspeed[0..7] Vertical scrolling speed of the background (pixels per step).
background_alpha[0..7] Transparency (alpha) value to use when drawing the background. A value of 1 is the normal setting; a value of 0 is completely transparent. Use with care. Drawing partially transparent backgrounds takes a lot of time and will slow down the game.