Flutter Flame priority value is not working

Viewed 16

I have SpriteComponent named Obstacle. I set the priority value to 0 in the super method. But the Obstacle component is regenerated with the update method. It disappears when it exits the screen. When it reoccurs, the priorty value becomes invalid. I am sharing sample screenshots.

first img
second img

The player stays behind the obstacle. GameOverPanel always at the back.

  @override
  Future<void> onLoad() async {
    add(ScreenHitbox());

    add(gameRef.homeBackgroundParallaxComponent);
    add(gameRef.homeBaseParallax);

    add(gameRef.obstacleManager..priority = 0);
    add(gameRef.playerManager..priority = 1);
    add(gameRef.gameOverPanel..priority = 2);
  }

  @override
  void update(double dt) {
    super.update(dt);
    gameRef.obstacleManager.updateObstacle(GameConst.gameSpeed * dt);
    gameRef.playerManager.changePositionDown(value: GameConst.gravity * dt);
  }
  • Game Over Panel
class GameOverPanel extends Component with HasGameRef<HopyBirdGame> {
  GameOverPanel() : super(priority: 4);

  @override
  Future<void> onLoad() async {
    add(GameOverText());
  }

  @override
  void renderTree(Canvas canvas) {
    if (gameRef.gameOver) {
      super.renderTree(canvas);
    }
  }
}

class GameOverText extends SpriteComponent with HasGameRef<HopyBirdGame> {
  GameOverText() : super(size: Vector2(300, 80), anchor: Anchor.center);

  @override
  Future<void>? onLoad() async {
    sprite = await gameRef.loadSprite(Assets.gameOver.path);
    position = Vector2(gameRef.screenWidth / 2, gameRef.screenHeight / 2);
    return super.onLoad();
  }
}
1 Answers

I'm guessing (since the code for the ObstacleManager isn't present) that you are adding the obstacles directly to the game from the ObstacleManager to the component tree and that you aren't adding the priority to the actual Obstacle components.

If this is not the case, please update the question with the code for the ObstacleManager.

Related