mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 19:12:31 +08:00
feat(flame): Add helper methods to create frame data on SpriteSheet (#2754)
Add two methods to SpriteSheet to create frame data for SpriteAnimation
This commit is contained in:
committed by
GitHub
parent
7313cd5352
commit
477221998a
@ -75,14 +75,14 @@ klondike/
|
||||
└─pubspec.yaml
|
||||
```
|
||||
|
||||
By the way, this kind of file is called the **spritesheet**: it's just a
|
||||
By the way, this kind of file is called the **sprite sheet**: it's just a
|
||||
collection of multiple independent images in a single file. We are using a
|
||||
spritesheet here for the simple reason that loading a single large image is
|
||||
sprite sheet here for the simple reason that loading a single large image is
|
||||
faster than many small images. In addition, rendering sprites that were
|
||||
extracted from a single source image can be faster too, since Flutter will
|
||||
optimize multiple such drawing commands into a single `drawAtlas` command.
|
||||
|
||||
Here are the contents of my spritesheet:
|
||||
Here are the contents of my sprite sheet:
|
||||
|
||||
- Numerals 2, 3, 4, ..., K, A. In theory, we could have rendered these in the
|
||||
game as text strings, but then we would need to also include a font as an
|
||||
|
||||
@ -39,9 +39,9 @@ have been more difficult to access that image from other classes.
|
||||
|
||||
Also note that I am `await`ing the image to finish loading before initializing
|
||||
anything else in the game. This is for convenience: it means that by the time
|
||||
all other components are initialized, they can assume the spritesheet is already
|
||||
all other components are initialized, they can assume the sprite sheet is already
|
||||
loaded. We can even add a helper function to extract a sprite from the common
|
||||
spritesheet:
|
||||
sprite sheet:
|
||||
|
||||
```dart
|
||||
Sprite klondikeSprite(double x, double y, double width, double height) {
|
||||
@ -193,7 +193,7 @@ not planning to change these values during the game:
|
||||
Next, we will create a `Stock` component, the `Waste`, four `Foundation`s and
|
||||
seven `Pile`s, setting their sizes and positions in the world. The positions
|
||||
are calculated using simple arithmetics. This should all happen inside the
|
||||
`onLoad` method, after loading the spritesheet:
|
||||
`onLoad` method, after loading the sprite sheet:
|
||||
|
||||
```dart
|
||||
final stock = Stock()
|
||||
|
||||
@ -71,9 +71,9 @@ after the image is loaded into the cache.
|
||||
```
|
||||
|
||||
The last four numbers in the constructor are the coordinates of the sprite
|
||||
image within the spritesheet `klondike-sprites.png`. If you're wondering how I
|
||||
image within the sprite sheet `klondike-sprites.png`. If you're wondering how I
|
||||
obtained these numbers, the answer is that I used a free online service
|
||||
[spritecow.com] -- it's a handy tool for locating sprites within a spritesheet.
|
||||
[spritecow.com] -- it's a handy tool for locating sprites within a sprite sheet.
|
||||
|
||||
Lastly, I have simple getters to determine the "color" of a suit. This will be
|
||||
needed later when we need to enforce the rule that cards can only be placed
|
||||
|
||||
@ -52,10 +52,10 @@ provide free pixel art that can be used in games, but please check and comply wi
|
||||
always provide valid creator attribution. For this game though, I am going to take a chance and
|
||||
make my artwork using an online pixel art tool. If you decide to use this tool, multiple online
|
||||
tutorials will assist you with the basic operations as well as exporting the assets. Now normally,
|
||||
most games will utilize spritesheets. These combine many images into one larger image that can be
|
||||
most games will utilize sprite sheets. These combine many images into one larger image that can be
|
||||
sectioned and used as individual images. For this tutorial though, I specifically will save the
|
||||
images individually as I want to demonstrate the Flame engine's caching abilities. Ember and the
|
||||
water enemy are spritesheets though as they contain multiple images to create animations.
|
||||
water enemy are sprite sheets though as they contain multiple images to create animations.
|
||||
|
||||
Right-click the images below, choose "Save as...", and store them in the `assets/images` folder of the
|
||||
project. At this point our project's structure looks like this:
|
||||
|
||||
Reference in New Issue
Block a user