【问题标题】:Flutter Flame SpriteComponent with Tapable does not recognize onTapDown MethodFlutter Flame SpriteComponent with Tapable 无法识别 onTapDown 方法
【发布时间】:2021-05-27 16:22:33
【问题描述】:

我正在为火焰菜单创建一些 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;
  }
}

【问题讨论】:

  • 你能发布更多的堆栈跟踪吗? “NoSuchMethod”在抱怨哪种方法?
  • 确切的消息是:方法 '+' 在 null 上被调用。接收者:null 尝试调用:+(Instance of 'Vector2')

标签: flutter game-engine flame


【解决方案1】:

您已经在StartButton 组件中定义了positionposition 已经在PositionComponent 中定义。 只需删除定义:

Vector2 position;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-30
    • 2021-04-23
    • 1970-01-01
    • 2021-03-27
    • 2021-02-28
    • 2021-11-21
    • 2016-04-19
    • 2017-07-24
    相关资源
    最近更新 更多