Flutter 为什么onTap方法在火焰中不起作用?
我试图用flame和Flatter重新创建flappy birds游戏,但我遇到了一个问题,我想知道是否有人能帮助我 问题是onTap方法不起作用。这是我的代码:Flutter 为什么onTap方法在火焰中不起作用?,flutter,flame,Flutter,Flame,我试图用flame和Flatter重新创建flappy birds游戏,但我遇到了一个问题,我想知道是否有人能帮助我 问题是onTap方法不起作用。这是我的代码: const COLOR = const Color(0xFF75DA8B); const SIZE = 52.0; const GRAVITY = 400.0; const BOOST = -380.0; void main() async{ WidgetsFlutterBinding.ensureInitialized ();
const COLOR = const Color(0xFF75DA8B);
const SIZE = 52.0;
const GRAVITY = 400.0;
const BOOST = -380.0;
void main() async{
WidgetsFlutterBinding.ensureInitialized ();
final size = await Flame.util.initialDimensions();
final game = MyGame(size);
runApp(game.widget);
}
class Bg extends Component with Resizable{
static final Paint _paint = Paint()..color = COLOR;
@override
void render(Canvas c) {
c.drawRect(Rect.fromLTWH(0.0, 0.0, size.width, size.height), _paint);
}
@override
void update(double t) {
}
}
class Bird extends AnimationComponent with Resizable{
double speedY = 0.0;
bool frozen;
Bird () : super.sequenced(SIZE, SIZE, 'bird.png', 4, textureWidth: 16.0, textureHeight: 16.0) {
this.anchor = Anchor.center;
}
Position get velocity => Position (300.0, speedY);
reset () {
this.x = size.width/ 2;
this.y = size.height/2;
speedY = 0;
frozen = true;
angle = 0.0;
}
@override
void resize(Size size) {
super.resize(size);
reset();
}
@override
void update(double t) {
super.update(t);
if (frozen) return;
this.y += speedY * t - GRAVITY * t * t / 2;
this.speedY += GRAVITY * t;
this.angle = velocity.angle();
if (y > size.height) {
reset();
}
}
onTap () {
if (frozen) {
frozen = false;
return;
}
speedY = (speedY + BOOST).clamp(BOOST, speedY);
}
}
class MyGame extends BaseGame {
Bird bird;
MyGame (Size size){
add(Bg());
add(bird = Bird());
}
@override
void onTap() {
bird.onTap();
}
}
鸟保持静止,如果我对代码行进行注释:
如果(冻结)退货;
在更新方法上,它会下降,但ontap不起作用
你知道为什么吗
提前非常感谢。我不知道您使用的是哪个版本的Flame,因为这是一个很老的问题。如果您现在正在使用1.0.0的候选版本,则至少应遵循以下结构:
class MyGame使用HastaTableComponents扩展了BaseGame{
Future onLoad()异步{
//加载图像和精灵等
添加(MyComponent());
}
…你的游戏代码
}
类MyComponent使用Tapable扩展SpriteAnimationComponent{
...
@凌驾
bool onTapUp(TapUpInfo事件){}
@凌驾
bool onTapDown(tappdowninfo事件){}
@凌驾
bool onTapCancel(){}
}