In this short tutorial I want to show you how to play 2D animations with GFX. The first thing to say is that 2D animations can only be played with the Sprite GameElement. For this we need a sprite itself as well as an animation sheet which represents the different frames. I picked an CC0 animation sheet for this tutorial which you can find in the attachments. Thanks to the artist wich created this sheet (Character sprite + walk animation | OpenGameArt.org).
As you can see we are creating a sprite in the first line. It is important that you give it a texture. You can use the animation sheet or a dummy texture. In the second line we create the AnimationBehaviour and specify how many columns and rows it has, the frame time and the texture. Then we use AddAnimation to define an animation within this sprite. In our case it is the animation for the downswing.
In the OnInit event of our game we then load an animation and play it. The animations can also be loaded, changed and played during the game.
C Source Code
- var sprite = new Sprite("sprite", new Vec3(0, 0), new Vec3(48, 48), this.AssetManager.GetTexture("ch003.png"));
- var animationBehavior = sprite.AddBehavior<AnimationBehavior>(new AnimationBehavior(4, 4, 50, this.AssetManager.GetTexture("ch003.png")));
- animationBehavior.AddAnimation(new Animation("WalkDown", 0, 0, 4));
In the OnInit event of our game we then load an animation and play it. The animations can also be loaded, changed and played during the game.