我目前正在玩基于 Flutter 的小型 2D 游戏引擎Flame 。我的代码包含一个相当奇怪的 Heisenbug:如果您通过run
or启动它,它工作正常debug
。但是如果你设置一个断点,它会抛出一个错误
Unhandled exception:
NoSuchMethodError: The getter 'cls' was called on null.
虽然我不认为这与火焰有关,但我无法单独重现颤振的问题。所以我包含了我的基于火焰的代码的最小版本。它只是用灰色背景颜色绘制电话屏幕:
主要飞镖:
import 'package:flutter/material.dart';
import 'package:temp/game.dart';
void main(){
MyGame game = MyGame(); // error thrown here
runApp(game.widget);
}
游戏飞镖:
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 中添加火焰:
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
并开始调试时,都会发生我的错误。