我目前正在玩flame,这是一种基于抖动的小型2D游戏引擎。我的代码包含一个相当奇怪的Heisenbug:如果您通过run
或debug
启动它,它将很好地工作。但是,如果设置断点,则会引发错误
Unhandled exception:
NoSuchMethodError: The getter 'cls' was called on null.
尽管我不认为这与火焰有关,但仅靠颤动我还是无法重现问题。因此,我包括基于火焰的代码的最低版本。它只是将电话屏幕涂成灰色背景颜色:
main.dart:
import 'package:flutter/material.dart';
import 'package:temp/game.dart';
void main(){
MyGame game = MyGame(); // error thrown here
runApp(game.widget);
}
game.dart:
import 'package:flame/game.dart';
import 'dart:ui';
class MyGame extends Game{
Size screenSize;
@override
void render(Canvas canvas) {
Rect screenRect = Rect.fromLTWH(0, 0, screenSize.width, screenSize.height);
Paint screenPaint = Paint();
screenPaint.color = Color.fromARGB(255, 100, 100, 100);
canvas.drawRect(screenRect, screenPaint);
}
@override
void update(double t) {
}
@override
void resize(Size size) {
super.resize(size);
screenSize = size;
}
}
如果要运行此命令,还必须在pubspec中添加Flame:
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
flame: ^0.10.2
每次在MyGame
的render方法中设置一个断点并开始调试时,都会发生我的错误。
答案 0 :(得分:0)
我无法在flame: ^0.22.0
上重现此内容,因此您可以升级到该版本,它应该可以正常工作。