长话短说。我在扩展窗口小部件中使用火焰引擎。现在,我正在加载六个背景以及我的SpriteComponent类,在游戏中用HasTappableComponents扩展FlameGame。现在我想在这个SpriteComponent中添加一些交互,比如在没有点击的情况下进行点击或者切换动画。我不明白为什么它连‘点击’都不打印出来...
class FoxCharacter extends SpriteComponent with Tappable, HasGameRef {
String working = "like a charm";
late SpriteAnimation animation;
late SpriteAnimationComponent animationComponent2;
Future<void> onLoad() async {
super.onLoad();
animation = await gameRef.loadSpriteAnimation(
'fox.png',
SpriteAnimationData.sequenced(
amount: 8,
textureSize: Vector2(64.0, 59.0),
stepTime: 0.15,
),
);
animationComponent2 = SpriteAnimationComponent(
animation: animation,
size: Vector2(200.0, 200.0),
position:Vector2(gameRef.size.x / 2 - 50, gameRef.size.y - 215),
);
add(animationComponent2);
print(working);
}
@override
bool onTapDown(TapDownInfo event) {
print("tap down");
return true;
}
}我试着把final改成late,以及其他一些无聊的想法,但都没有结果。如何向此FoxCharacter添加交互组件?有没有可能它不能在SpriteComponent中使用,而只能在PositionComponent中使用?
发布于 2021-11-03 22:31:19
这是因为您的SpriteComponent (FoxCharacter)没有大小或hitbox,所以事件系统不知道如何检查它是否应该传递事件。它确实有一个子对象(animationComponent2),这是您可以在屏幕上看到的。
在我看来,您需要使用SpriteAnimationGroupComponent并设置该组件的位置和大小,然后添加要在该组件中交换的所有不同动画。
当然,您可以只将相同的size和position设置为SpriteComponent,而不是将它们传递给animationComponent2,但我认为在该部分完成后,您还会遇到其他问题。
您可以查看SpriteAnimationGroupComponent here的文档
https://stackoverflow.com/questions/69827991
复制相似问题