What is Sprite
Sprites or graphics are simple PNG or JPEG files that you insert into your game. Although we could use animation, we are going to use a simple single frame of the sprite sheet for this blog.
Steps to use sprite
Adding image you want to use
Copy a single frame(any single image of the above format) into your assets image folder. For this blog, I have two images in assets/images folder, one is of a girl and the other is of a boy. As shown below
Register to Pubscpec.yaml file
Go to pubspec file and, under assets, register your folder.
Loading sprites
Spritecomponent girl is a variable to use sprite component model from flame. We can decide running status as well as direction and speed too.
As we have added two pics one is of girl and of a boy I am here adding girl for sprite animation we can set Area in which we want our animation to take place as well as starting coordinates. You can position that, of course, as per your choice.
positioning of image is done as shown below, next thing we want to animate that pic as per the above coordinates mentioned and in the mentioned area.
Moving that pic on screen
We can move that pic on screen using x-y coordinate system. To make her (the pic loaded) go down we are going to have to add 1 to the y-axis of her position, her position is based on a grid it's x for horizontal movement and y for up and down movement.
Key concept: Flutter game using flame uses a loop, so to update the image location you will have to update it within the loop. It is just a simple y edition.
Here we want the girl to move down, so we have to take y from the x-y coordinate system and put it into an update loop and have her drop down within the myGame class override existing update method that is included with the base game as shown below code.
Watch the video down to see how this piece of code works.
As we can see, the girl is going down with no limits, so we must bound her to not move out of the screen. So we have to control the movement of her.
For controlling the movement import
import 'package:flame/gestures.dart'
That's it for this blog, next blog we will see sprite animation sheet and entry of the second character in the game.