Flame flutter 中的 HasTappableComponents 类已弃用

问题描述 投票:0回答:1

onTapDown
中的
GamePlayScreen
方法似乎没有被触发,我不明白为什么。任何见解或建议将不胜感激。

class FlyingCrowGameStart extends FlameGame with TapCallbacks {
  late final RouterComponent router;
  bool gameOver = false;
  bool showingGameOverScreen = false;
  Vector2 gravity = Vector2(0, 30);

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    add(router = RouterComponent(initialRoute: 'gameStart', routes: {
      'gameStart': Route(GamePlayScreen.new),
      'gameEnd': Route(GameEndScreen.new),
    }));

    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
    ]);
  }

  @override
  void onTapDown(TapDownEvent event) {
    super.onTapDown(event);
    gravity.y -= 20;
    print('main');
  }
}

class GamePlayScreen extends Component with HasGameRef<FlyingCrowGameStart>, TapCallbacks {
  FlyingCrow flyingCrow = FlyingCrow();

  @override
  Future<void> onLoad() async {
    super.onLoad();
    ParallaxComponent flyingCrowBackground = await gameRef.loadParallaxComponent(
      [
        ParallaxImageData('layers/sky.png'),
        // ... (other image data)
      ],
      baseVelocity: Vector2(5, 0),
      velocityMultiplierDelta: Vector2(1.6, 1.0),
    );
    add(flyingCrowBackground);
    add(flyingCrow);
    add(EnemyCraft());
  }

  void containsLocalPoints(Vector2 point) => true;

  @override
  void onTapDown(TapDownEvent event) {
    super.onTapDown(event);
    gameRef.gravity.y -= 20;
    print('play game');
  }
}
flutter gesture flame
1个回答
0
投票

Component
containsPoint
上返回 false,因为它没有任何大小等。 您可以在这里使用的是
World
:

class FlyingCrowGameStart extends FlameGame with TapCallbacks {
  late final RouterComponent router;
  bool gameOver = false;
  bool showingGameOverScreen = false;
  Vector2 gravity = Vector2(0, 30);

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    add(router = RouterComponent(initialRoute: 'gameStart', routes: {
      'gameStart': Route(GamePlayScreen.new),
      'gameEnd': Route(GameEndScreen.new),
    }));

    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
    ]);
  }

  @override
  void onTapDown(TapDownEvent event) {
    super.onTapDown(event);
    gravity.y -= 20;
    print('main');
  }
}

class GamePlayScreen extends World with HasGameRef<FlyingCrowGameStart>, TapCallbacks {
  FlyingCrow flyingCrow = FlyingCrow();

  @override
  Future<void> onLoad() async {
    super.onLoad();
    ParallaxComponent flyingCrowBackground = await gameRef.loadParallaxComponent(
      [
        ParallaxImageData('layers/sky.png'),
        // ... (other image data)
      ],
      baseVelocity: Vector2(5, 0),
      velocityMultiplierDelta: Vector2(1.6, 1.0),
    );
    add(flyingCrowBackground);
    add(flyingCrow);
    add(EnemyCraft());
  }

  @override
  void onMount() {
    // A bit of a hack until the `WorldRoute` is released.
    parent = game.world;
  }

  void containsLocalPoints(Vector2 point) => true;

  @override
  void onTapDown(TapDownEvent event) {
    super.onTapDown(event);
    gameRef.gravity.y -= 20;
    print('play game');
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.