1. Guides
  2. Sprites

Guides

Sprites

A sprite acts like an animated image, we can use it to represent players or game objects.

Designing a sprite image

Before inserting a sprite, you will need to manually design it, with a tool like microsoft paint, gimp, etc. In the end you should have an image like this:

Phaser.js

example image from phaser.js tutorial

loading the image

You can load the sprite image as you open a normal image: See image guid.

        gmImage img = gm_image_open("assets/sprites/player.png");

      

if assets/sprites/player.png is the path to your player image in assets folder.

Creating the sprite

        gmSprite player = gm_sprite_create(img, 9);

      

[gm_sprite_create] creates a sprite using img and with the specified number of slices(sub images).

You may also create a sprite from an image file directly with gm_sprite_open

animating the sprite

For the sprite to move and switch between it's images you are going to create gmSpriteAnim objects, See gmspriteanim reference,

you can create an animation using gm_sprite_anim_create.

Example

        // put the path to the image sprite
gmSprite player = gm_sprite_open("assets/sprites/player.png", 9);

gmSpriteAnim
    player_walk_anim = gm_sprite_anim_create(0.2, "abcdcb"),
    player_jump_anim = gm_sprite_anim_create(0.2, "defgh");

player.animation = player_walk_anim;

gm_sprite_draw(&player, 0, 0, 0.1, 0.1);

      

Reference

For more information on sprites, see sprite.h reference