我下面的撲火焰教程這里并能得到我的代碼編譯并運行在模擬器(iOS和鉻),但是沒有出現在應用程式運行時。我假設我的 crate.png 的影像應該顯示出來,但我看到的只是一個黑屏。
這是我的 main.dart:
import 'dart:math' as math;
import 'dart:ui';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:flame/components.dart';
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
class MyCrate extends SpriteComponent {
// creates a component that renders the crate.png sprite, with size 16 x 16
MyCrate() : super(size: Vector2.all(16));
Future<void> onl oad() async {
sprite = await Sprite.load('crate.png');
anchor = Anchor.center;
}
@override
void onGameResize(Vector2 gameSize) {
// We don't need to set the position in the constructor, we can it directly here since it will
// be called once before the first time it is rendered.
position = gameSize / 2;
}
}
class MyGame extends FlameGame {
MyGame() {
add(MyCrate());
}
}
main() {
final myGame = MyGame();
runApp(
GameWidget(
game: myGame,
),
);
}
這是我的 pubspec.yaml:
name: boxgame
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0 1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
flame: ^1.0.0-releasecandidate.16
dashbook: ^0.1.5
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0
flutter:
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/crate.png
我在專案根目錄中創建了我的資產檔案夾,并將其放置在名為影像的檔案夾中。
編輯
我剛剛意識到有一些錯誤輸出:
Error: Assertion failed:
../…/components/component.dart:323
parentGame.hasLayout
"\"prepare/add\" called before the game is ready. Did you try to access it on the Game constructor? Use the \"onLoad\" ot \"onParentMethod\" method instead."
at Object.throw_ [as throw] (http://localhost:53993/dart_sdk.js:5083:11)
at Object.assertFailed (http://localhost:53993/dart_sdk.js:5008:15)
at main.MyCrate.new.prepare (http://localhost:53993/packages/flame/src/game/mixins/fps_counter.dart.lib.js:2509:41)
at component_set.ComponentSet.new.addChild (http://localhost:53993/packages/flame/src/game/mixins/fps_counter.dart.lib.js:7041:19)
at addChild.next (<anonymous>)
at runBody (http://localhost:53993/dart_sdk.js:40127:34)
at Object._async [as async] (http://localhost:53993/dart_sdk.js:40158:7)
at component_set.ComponentSet.new.addChild (http://localhost:53993/packages/flame/src/game/mixins/fps_counter.dart.lib.js:7040:20)
at main.MyGame.new.add (http://localhost:53993/packages/flame/src/game/mixins/fps_counter.dart.lib.js:2453:28)
at new main.MyGame.new (http://localhost:53993/packages/boxgame/main.dart.lib.js:74:10)
at main$ (http://localhost:53993/packages/boxgame/main.dart.lib.js:84:18)
at main (http://localhost:53993/web_entrypoint.dart.lib.js:36:29)
at main.next (<anonymous>)
at http://localhost:53993/dart_sdk.js:40108:33
at _RootZone.runUnary (http://localhost:53993/dart_sdk.js:39979:59)
at _FutureListener.thenAwait.handleValue (http://localhost:53993/dart_sdk.js:34926:29)
at handleValueCallback (http://localhost:53993/dart_sdk.js:35493:49)
at Function._propagateToListeners (http://localhost:53993/dart_sdk.js:35531:17)
at _Future.new.[_completeWithValue] (http://localhost:53993/dart_sdk.js:35379:23)
at http://localhost:53993/dart_sdk.js:34563:46
at _RootZone.runUnary (http://localhost:53993/dart_sdk.js:39979:59)
at _FutureListener.then.handleValue (http://localhost:53993/dart_sdk.js:34926:29)
at handleValueCallback (http://localhost:53993/dart_sdk.js:35493:49)
at Function._propagateToListeners (http://localhost:53993/dart_sdk.js:35531:17)
at _Future.new.[_completeWithValue] (http://localhost:53993/dart_sdk.js:35379:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:53993/dart_sdk.js:35400:35)
at Object._microtaskLoop (http://localhost:53993/dart_sdk.js:40246:13)
at _startMicrotaskLoop (http://localhost:53993/dart_sdk.js:40252:13)
at http://localhost:53993/dart_sdk.js:35751:9
我在呼叫 add 之前嘗試在負載上運行,但我遇到了同樣的錯誤:
class MyGame extends FlameGame {
MyGame() {
MyCrate crate = MyCrate();
crate.onLoad();
add(crate);
}
}
uj5u.com熱心網友回復:
您找到的解決方案作業正常,如果您想修復原始代碼,您必須遵循堆疊跟蹤中稍微神秘的訊息:
“準備/添加”在游戲準備好之前呼叫。您是否嘗試在 Game 建構式上訪問它?請改用“onLoad”或“onMount”方法。
這意味著您必須將add組件的ing移動到 ,onLoad而不是像現在那樣將它們放在游戲建構式中。所以它看起來像這樣:
class MyCrate extends SpriteComponent {
// creates a component that renders the crate.png sprite, with size 16 x 16
MyCrate() : super(size: Vector2.all(16));
Future<void> onl oad() async {
sprite = await Sprite.load('crate.png');
anchor = Anchor.center;
}
@override
void onGameResize(Vector2 gameSize) {
// We don't need to set the position in the constructor, we can it directly here since it will
// be called once before the first time it is rendered.
position = gameSize / 2;
}
}
class MyGame extends FlameGame {
Future<void> onl oad() async {
await super.onLoad();
add(MyCrate());
}
}
main() {
final myGame = MyGame();
runApp(
GameWidget(
game: myGame,
),
);
}
uj5u.com熱心網友回復:
您應該修復資產的地址。檢查以下示例:
Future<void> onl oad() async {
sprite = await Sprite.load('assets/images/crate.png');
anchor = Anchor.center;
}
uj5u.com熱心網友回復:
我不確定為什么會這樣,但以下內容使影像顯示出來:
class MyGame extends FlameGame {
late final SpriteComponent player;
@override
Future<void> onl oad() async {
final sprite = await Sprite.load('crate.png');
final size = Vector2.all(16.0);
final player = SpriteComponent(size: size, sprite: sprite);
// screen coordinates
player.position = Vector2(0.0, 0.0);
player.angle = 0; // 0 by default, can also be set in the constructor
add(player); // Adds the component
}
}
main() {
final myGame = MyGame();
runApp(
GameWidget(
game: myGame,
),
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/350635.html
