Flutter:flame, ComposedComponent 不能混合到 PositionComponent 上

问题描述

我正在用 Flutter 制作游戏,我在 github https://github.com/g0rdan/Flutter.Bird 中找到了这个链接,我试图在我的电脑上运行它,但我遇到了这个错误 error: 'ComposedComponent' can'不要混入“PositionComponent”,因为“PositionComponent”没有实现“HasGameRef”。 (mixin_application_not_implemented_interface at [myfirstgame] lib\game\bird.dart:16)

enter image description here

enum BirdStatus { waiting,flying}
enum BirdFlyingStatus { up,down,none }

class Bird extends PositionComponent with ComposedComponent { == from this line 
  int _counter = 0;
  int _movingUpSteps = 15;
  Size _screenSize;
  double _heightDiff = 0.0;
  double _stepDiff = 0.0;

  BirdGround ground;
  BirdStatus status = BirdStatus.waiting;
  BirdFlyingStatus flyingStatus = BirdFlyingStatus.none;

  Bird(Image spriteImage,Size screenSize)
  {
    _screenSize = screenSize;
    List<Sprite> sprites = [
      Sprite.fromImage(
        spriteImage,width: SpriteDimensions.birdWidth,height: SpriteDimensions.birdHeight,y: SpritesPostions.birdSprite1Y,x: SpritesPostions.birdSprite1X,),Sprite.fromImage(
        spriteImage,y: SpritesPostions.birdSprite2Y,x: SpritesPostions.birdSprite2X,y: SpritesPostions.birdSprite3Y,x: SpritesPostions.birdSprite3X,)
    ];

    var animatedBird = new Animation.spriteList(sprites,stepTime: 0.15);
    this.ground = BirdGround(animatedBird);
    this..add(ground);
  }

  void setPosition(double x,double y) {
    this.ground.x = x;
    this.ground.y = y;
  }

  void update(double t) {
    if (status == BirdStatus.flying) {
      _counter++;
      if (_counter <= _movingUpSteps) {
        flyingStatus = BirdFlyingStatus.up;
        this.ground.showAnimation = true;
        this.ground.angle -= 0.01;
        this.ground.y -= t * 100 * getSpeedratio(flyingStatus,_counter);
      }
      else {
        flyingStatus = BirdFlyingStatus.down;
        this.ground.showAnimation = false;

        if (_heightDiff == 0)
          _heightDiff = (_screenSize.height - this.ground.y);
        if (_stepDiff == 0)
          _stepDiff = this.ground.angle.abs() / (_heightDiff / 10);

        this.ground.angle += _stepDiff;
        this.ground.y += t * 100 * getSpeedratio(flyingStatus,_counter);
      }
      this.ground.update(t);
    }
  }

  double getSpeedratio(BirdFlyingStatus flyingStatus,int counter){
    if (flyingStatus == BirdFlyingStatus.up) {
      var backwardCounter = _movingUpSteps - counter;
      return backwardCounter / 10.0;
    }
    if (flyingStatus == BirdFlyingStatus.down) {
      var diffCounter = counter - _movingUpSteps;
      return diffCounter / 10.0;
    }
    return 0.0;
  }

  void jump() {
    Flame.audio.play('wing.wav');
    status = BirdStatus.flying;
    _counter = 0;
    this.ground.angle = 0;
  }
}

class BirdGround extends AnimationComponent {
  bool showAnimation = true;

  BirdGround(Animation animation)
      : super(ComponentDimensions.birdWidth,ComponentDimensions.birdHeight,animation);

  @override
  void update(double t){
    if (showAnimation) {
      super.update(t);
    }
  }
}

解决方法

这使用了一个非常旧的 Flame 版本,所以我建议不要在它之上构建任何东西。

但是对于您的问题,它在您的组件上缺少 HasGameRef 混合,因此如果您编写这样的内容,它应该可以工作:

class Bird extends PositionComponent with HasGameRef<YourGameClass>,ComposedComponent { ...

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...