How to Sort Sub-Images in GameMaker: Studio - dummies

How to Sort Sub-Images in GameMaker: Studio

By Michael Rohde

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.

  1. 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.

  2. Choose Resources→Create Object.

    The Object Properties window appears.

  3. In the Name field, type something like obj_brick.

  4. 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.

  5. Choose Add Event→Create.

    A Create Event appears in the Events section of the Object Properties window (refer to the figure).


  6. Drag and drop the Execute Code Action from the Control tab into the Actions section.

    An empty code window appears.

  7. In the code window, type the following code:

    image_speed = 0;
    if y <= 32 image_index = 0;
    if y <= 64 image_index = 1;
    if y <= 96 image_index = 2;
    if y <= 128 image_index = 3;
    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.
    An example of sorted bricks using the index of sub-images.
  8. 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.