gpt4 book ai didi

flutter - Flutter:并非总是调用onTapDown函数

转载 作者:行者123 更新时间:2023-12-03 03:54:41 24 4
gpt4 key购买 nike

我是 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");
}

}

最佳答案

确保使用Flame 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);
}
}

另外,请记住,如果仅对它们调用super,则不必覆盖 updateresizerender

关于flutter - Flutter:并非总是调用onTapDown函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61835491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com