How to Sort Sub-Images in GameMaker: Studio
If you want to use a single Object to display multiple Instances within a Room in GameMaker: Studio, you can assign a Sprite to that Object that has multiple sub-images. Then you can write the following code so that GameMaker displays the sub-images in a sorted manner. The following code example is from the in-software coding tutorial, which shows you how to make a breakout-style game.
Create a Sprite with at least four sub-images (for example, each sub-image could be a different colored brick).
The Sprite should appear in the Resource tree.
Choose Resources→Create Object.
The Object Properties window appears.
In the Name field, type something like obj_brick.
From the Sprite drop-down list, select the Sprite with the sub-images.
A thumbnail of the image appears in the Sprite section, and the name of the image appears in the field.
Choose Add Event→Create.
A Create Event appears in the Events section of the Object Properties window (refer to the figure).
Drag and drop the Execute Code Action from the Control tab into the Actions section.
An empty code window appears.
In the code window, type the following code:
image_speed = 0; if y <= 32 image_index = 0; else if y <= 64 image_index = 1; else if y <= 96 image_index = 2; else if y <= 128 image_index = 3; else if y <= 160 image_index = 4;
The code checks for the Y position of the Instance (as determined by the coordinates of the Room) and then assigns the index number of the Sprite to determine which sub-image to display.An example of sorted bricks using the index of sub-images.
Click the green check mark to save and close.
The figure shows an example of sorting sub-images by index using this code. This game uses a single Sprite with five sub-images, which are displayed based on their index.