带有 Tapable 的 Flutter Flame SpriteComponent 无法识别 onTapDown 方法
Flutter Flame SpriteComponent with Tapable does not recognize onTapDown Method
我正在为 flame 中的菜单创建一些 UI 按钮。除了在 onTapDown 事件上更改 sprite 之外,按钮本身能够很好地呈现。将 class 添加到 basegame 组件后,应用程序无法找到 ontapdown 事件。我只收到错误 'NoSuchMethod'.
编辑:我正在使用 flame releasecandidate.11 和 flame audio 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;
}
}
您已在 StartButton
组件中定义 position
,position
已在 PositionComponent
中定义。
只需删除定义:
Vector2 position;
以及您设置它的行 (this.position = position;
),因为当您使用 super(position: position)
.
将它传递给 super 时它已经设置好了
我正在为 flame 中的菜单创建一些 UI 按钮。除了在 onTapDown 事件上更改 sprite 之外,按钮本身能够很好地呈现。将 class 添加到 basegame 组件后,应用程序无法找到 ontapdown 事件。我只收到错误 'NoSuchMethod'.
编辑:我正在使用 flame releasecandidate.11 和 flame audio 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;
}
}
您已在 StartButton
组件中定义 position
,position
已在 PositionComponent
中定义。
只需删除定义:
Vector2 position;
以及您设置它的行 (this.position = position;
),因为当您使用 super(position: position)
.