1

我正在为火焰菜单创建一些 UI 按钮。除了在 onTapDown 事件上更改精灵之外,按钮本身能够很好地呈现。将类添加到 basegame 组件后,应用程序无法找到 ontapdown 事件。我只收到错误“NoSuchMethod”。

编辑:我正在使用火焰 releasecandidate.11 和火焰音频 rc.1。完整的错误信息是:

The following NoSuchMethodError was thrown while handling a gesture:
The method '+' was called on null.
Receiver: null
Tried calling: +(Instance of 'Vector2')

When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      Anchor.toOtherAnchorPosition (package:flame/src/anchor.dart:61:23)
#2      PositionComponent.topLeftPosition (package:flame/src/components/position_component.dart:57:19)
#3      PositionComponent.absoluteTopLeftPosition (package:flame/src/components/position_component.dart:75:14)
#4      PositionComponent.toAbsoluteRect (package:flame/src/components/position_component.dart:125:28)
...
Handler: "onTapDown"
Recognizer: MultiTapGestureRecognizer#13ffa
class ScrollGame extends BaseGame with HasTapableComponents {
  @override
  Future<void> onLoad() async {

    // Buttons
    add(StartButton(Vector2(canvasSize.x/2, canvasSize.y/2)));
  }
}

class StartButton extends PositionComponent with Tapable {
  bool isDown = false;
  Sprite untappedSpr;
  Sprite tappedSpr;
  Vector2 position;

  StartButton(Vector2 position) : super(position: position);

  @override
  Future<void> onLoad() async {
    final untapped = await Flame.images.load('Untapped.png');
    final tapped = await Flame.images.load('Tapped.png');

    untappedSpr = Sprite(untapped);
    tappedSpr = Sprite(tapped);

    this.size = Vector2(80, 25);
    this.position = position;
    this.anchor = Anchor.center;
  }

  @override
  void render(Canvas canvas) {
    super.render(canvas);
    final buttonSpr = isDown ? tappedSpr : untappedSpr;
    buttonSpr.render(canvas);
  }

  @override
  bool onTapUp(_) {
    isDown = false;
    return true;
  }

  @override
  bool onTapDown(_) {
    isDown = true;
    return true;
  }
}
4

1 回答 1

3

您已positionStartButton组件position中定义,已在PositionComponent. 只需删除定义:

Vector2 position;

以及您设置它的行 ( this.position = position;),因为当您将它传递给 super with 时它已经设置好了super(position: position)

于 2021-05-27T20:10:17.763 回答