loadFrames static method
Loads multiple frames from a single Texture (sprite sheet).
This method divides the texture
into a grid of frames based on the
provided frameWidth
, frameHeight
, and optional paddingX
and paddingY
.
Each valid frame is created as an Image instance.
texture
: The sprite sheet Texture.frameWidth
: The width of each individual frame.frameHeight
: The height of each individual frame.paddingX
: Horizontal padding between frames (defaults to 0).paddingY
: Vertical padding between frames (defaults to 0).pivotX
: The default horizontal pivot for all loaded frames (defaults to 0.5).pivotY
: The default vertical pivot for all loaded frames (defaults to 0.5).
If the texture
is still loading, frame extraction is deferred until the
texture has finished loading.
Returns an Images list (a List<Image>
) containing all extracted frames.
Implementation
static Images loadFrames({
required Texture texture,
required int frameWidth,
required int frameHeight,
int paddingX = 0,
int paddingY = 0,
double pivotX = 0.5,
double pivotY = 0.5,
}) {
final Images images = [];
grabFrames(Texture texture) {
final maxFramesX = (texture.width + paddingX) ~/ (frameWidth + paddingX);
final maxFramesY = (texture.height + paddingY) ~/ (frameHeight + paddingY);
if (maxFramesX <= 0 || maxFramesY <= 0) {
return images;
}
for (int row = 0; row < maxFramesY; row++) {
for (int col = 0; col < maxFramesX; col++) {
final currentX = (col * (frameWidth + paddingX));
final currentY = (row * (frameHeight + paddingY));
if (currentX + frameWidth <= texture.width && currentY + frameHeight <= texture.height) {
final sourceRect = Rect(currentX, currentY, frameWidth, frameHeight);
images.add(Image(texture: texture, sourceRect: sourceRect, pivotX: pivotX, pivotY: pivotY));
}
}
}
}
if (!texture.isLoading) {
grabFrames(texture);
} else {
texture.onLoad((Texture texture) {
grabFrames(texture);
});
}
return images;
}