如何在 Flame 中加载网络图像?

How to load network image in Flame?

在 Flame 文档中,图像 class 只需从资产文件夹加载。

bgSprite = Sprite('avatar/avatar-sample.png');

如何在 Flame 中加载网络图像。

您可以在 1.0.0-rc16 中执行类似的操作:

import dart:ui; // This is the package you want the Image class from, there are several

Future<Image> getImage(String path) async {
  Completer<ImageInfo> completer = Completer();
  var img = new NetworkImage(path);
  img.resolve(ImageConfiguration()).addListener(ImageStreamListener((ImageInfo info,bool _){
    completer.complete(info);
  }));
  ImageInfo imageInfo = await completer.future;
  return imageInfo.image;
}

然后在您的 onLoad 方法中,启动 bgSprite:

@override
Future<void> onLoad() async {
  final image = await getImage("your-url.com/sample.png");
  bgSprite = Sprite(image);
}

0.28.0 中,看起来你是 运行,你只需将最后一行替换为(但我真的建议升级到 1.0.0-rc16):

bgSprite = Sprite.fromImage(image);