如何改变火焰中的资产位置结构?

问题描述 投票:0回答:2

我想更改 Flame 使用的资源的默认结构。文档说

如果您想更改此结构,可以通过使用 prefix 参数并创建您自己的 AssetsCache、ImagesCache、AudioCache 和 SoundPools 实例,而不是使用 Flame 提供的全局实例。

上述文档页面

我知道我们可以使用前缀参数,但我不知道如何或在哪里访问共享实例,所以我可以实现这个结果。

flutter dart assets flame
2个回答
1
投票

目前在 Flame 中有两个实例化缓存的地方,首先我们有来自

FlameGame
mixin 的
Game
中的缓存,可以通过覆盖它们来更改它们:

class MyGame extends FlameGame {
  @override
  final images = Images(prefix: 'assets/special_images/');
  final assets = AssetsCache(prefix: 'assets/special_assets/');
  
  ...
}

然后我们在

Flame.dart
中有全局缓存:

class Flame {
  /// Flame asset bundle, defaults to the root bundle but can be globally
  /// changed.
  static AssetBundle bundle = rootBundle;

  /// Access a shared instance of [AssetsCache] class.
  static AssetsCache assets = AssetsCache();

  /// Access a shared instance of the [Images] class.
  static Images images = Images();

  /// Access a shared instance of the [Device] class.
  static Device device = Device();
}

您可以将这些缓存重新分配给具有所需前缀的新缓存。


0
投票

在构造函数之后使用更改

assets
images
来处理我的代码

class MyGame extends FlameGame {
  MyGame(){
    Flame.assets = AssetsCache(prefix: 'path/to/assets/');
    Flame.images = Images(prefix: 'path/to/images/');
  }

  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.