1

我是颤振开发的新手。如果我在屏幕上执行任何单击操作,我会添加 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");
    }

  }
4

1 回答 1

2

确保你使用火焰 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,如果您只对它们调用 super。resizerender

于 2020-06-08T16:57:05.367 回答