我是flutter开发的新手。我添加了onTapdown监听器,如果我在屏幕上执行任何点击的动作。它的工作,但问题是有时当我点击时,onTapdown函数没有被调用,我不知道我做了什么问题,希望能帮助我解决这个问题。
import 'package:flame/game.dart';
import 'package:flame/components/parallax_component.dart';
import 'package:flame/util.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Util flameUtil = Util();
await flameUtil.fullScreen();
runApp(MyGame(flameUtil).widget);
}
class MyGame extends BaseGame {
MyGame(Util flameUtil) {
final images = [
ParallaxImage("space/bg_base.png",repeat: ImageRepeat.repeat,fill: LayerFill.height),
ParallaxImage("space/bg_big_star.png",repeat: ImageRepeat.repeatY,fill: LayerFill.height),
ParallaxImage("space/bg_planet.png",repeat: ImageRepeat.repeat,fill: LayerFill.none),
];
var game = Squres(images);
add(game);
TapGestureRecognizer tapper = TapGestureRecognizer();
tapper.onTapDown = game.onTapDown;
flameUtil.addGestureRecognizer(tapper);
}
}
class Squres extends ParallaxComponent{
Squres(List<ParallaxImage> images) : super(images){
baseSpeed = const Offset(4,0);
layerDelta = const Offset(0,-50);
}
@override
void render(Canvas canvas) {
super.render(canvas);
}
@override
void resize(Size size) {
super.resize(size);
}
@override
void update(double t) {
super.update(t);
}
//not always trigger
void onTapDown(TapDownDetails tap){
print("trigger");
}
}
确保你使用火焰0.22.0^,并使用创建你的游戏类,而不是这样。
class MyGame extends BaseGame with TapDetector {
Squres game;
MyGame() {
final images = [
ParallaxImage("space/bg_base.png",repeat: ImageRepeat.repeat,fill: LayerFill.height),
ParallaxImage("space/bg_big_star.png",repeat: ImageRepeat.repeatY,fill: LayerFill.height),
ParallaxImage("space/bg_planet.png",repeat: ImageRepeat.repeat,fill: LayerFill.none),
];
game = Squres(images);
add(game);
}
@override
void onTapDown(TapUpDetails details) {
print("trigger");
game.onTapDown(details);
}
}
另外,请记住,你不需要重写... update
, resize
和 render
如果你只叫超级对他们。