我正在使用 Flutter 和 Flame (0.29.3) 构建一个简单的手机游戏。我正在尝试检测 PositionComponent/SpriteComponent 上的点击。但是,我没有这样做。参考/教程正在使用 addGestureRecognizer 但它已被弃用。
我定义我的 PositionComponent 如下;
class Enemy extends PositionComponent with Tapable {
Rect enemyRect;
Enemy(double x, double y) {
enemyRect = Rect.fromLTWH(x, y, 50, 50);
}
@override
void render(Canvas c) {
Color color = Color(0XFFFF0000);
Paint enemyColor = Paint()..color = color;
c.drawRect(enemyRect, enemyColor);
}
void update(double t) {}
@override
void onTapUp(TapUpDetails details) {
print("tap up");
}
@override
void onTapDown(TapDownDetails details) {
print("tap down");
}
@override
void onTapCancel() {
print("tap cancel");
}
}
而且,我将 PositionComponent 添加到我的游戏中。
class GameController extends BaseGame with HasTapableComponents {
Size screenSize;
double tileSize;
Player player;
Enemy enemy;
TapableComponent a;
GameController() {
initialize();
}
void initialize() async {
resize(await Flame.util.initialDimensions());
add(enemy = Enemy(200, 200));
}
@override
void render(Canvas c) {
Rect background = Rect.fromLTWH(0, 0, screenSize.width, screenSize.height);
Paint backgroundPaint = Paint()..color = Color(0xFFFAFAFA);
enemy.render(c);
}
@override
void update(double t) {}
@override
void resize(Size size) {
screenSize = size;
tileSize = screenSize.width / 10;
}
}
但是,它不起作用,我错过了什么吗?