mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 04:18:25 +08:00
Creating AssetsCache class
This commit is contained in:
@ -16,7 +16,7 @@ class MyGame extends BaseGame {
|
||||
|
||||
final animation = await FlameAnimation.Animation.fromAsepriteData(
|
||||
"chopper.png",
|
||||
"./assets/chopper.json"
|
||||
"chopper.json"
|
||||
);
|
||||
final animationComponent = AnimationComponent(200, 200, animation);
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
|
||||
import 'flame.dart';
|
||||
import 'sprite.dart';
|
||||
|
||||
/// Represents a single animation frame.
|
||||
@ -110,7 +110,7 @@ class Animation {
|
||||
/// [imagePath]: Source of the spritesheet animation
|
||||
/// [dataPath]: Animation's exported data in json format
|
||||
static Future<Animation> fromAsepriteData(String imagePath, String dataPath) async {
|
||||
String content = await rootBundle.loadString(dataPath);
|
||||
String content = await Flame.assets.readFile(dataPath);
|
||||
Map<String, dynamic> json = jsonDecode(content);
|
||||
|
||||
Map<String, dynamic> jsonFrames = json["frames"];
|
||||
|
||||
25
lib/assets_cache.dart
Normal file
25
lib/assets_cache.dart
Normal file
@ -0,0 +1,25 @@
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
|
||||
class AssetsCache {
|
||||
Map<String, String> textFiles = {};
|
||||
|
||||
void clear(String file) {
|
||||
textFiles.remove(file);
|
||||
}
|
||||
|
||||
void clearCache() {
|
||||
textFiles.clear();
|
||||
}
|
||||
|
||||
Future<String> readFile(String fileName) async {
|
||||
if (!textFiles.containsKey(fileName)) {
|
||||
textFiles[fileName] = await _readFile(fileName);
|
||||
}
|
||||
|
||||
return textFiles[fileName];
|
||||
}
|
||||
|
||||
Future<String> _readFile(String fileName) async {
|
||||
return await rootBundle.loadString("assets/" + fileName);
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,7 @@ import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'images.dart';
|
||||
import 'assets_cache.dart';
|
||||
import 'util.dart';
|
||||
|
||||
/// This class holds static references to some useful objects to use in your game.
|
||||
@ -27,6 +28,9 @@ class Flame {
|
||||
/// Access a shared instance of the [Util] class.
|
||||
static Util util = new Util();
|
||||
|
||||
/// Access a shard instance of [AssetsCache] class.
|
||||
static AssetsCache assets = new AssetsCache();
|
||||
|
||||
static Future<void> init(
|
||||
{AssetBundle bundle,
|
||||
bool fullScreen = true,
|
||||
|
||||
Reference in New Issue
Block a user