1

在 Flame 文档中,Image 类只是从资产文件夹中加载。

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

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

4

1 回答 1

2

你可以做这样的事情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);
于 2020-12-23T09:12:05.793 回答