long story short. I'm using flame engine inside expanded widget. Right now I'm loading six backgrounds as well as my SpriteComponent class inside Game extending FlameGame with HasTappableComponents. Now I want to add some interaction to this SpriteComponent, like reaction to tapping or switch animation while no tapping takes place. I don't understand why it doesn't even print that 'tap down'...
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;
}
}
I tried to change final to late and some other dull ideas with no results. How can I add interaction to this FoxCharacter? Is this possible that it cannot be used in SpriteComponent and only PositionComponent?
It is because your SpriteComponent
(FoxCharacter) doesn't have a size or a hitbox so the event system doesn't know how to check whether it should be passed events or not. It does have a child (animationComponent2), which is what you can see on the screen.
It looks to me like you'd want to use SpriteAnimationGroupComponent
and set the position and size on that one and then add all the different animations that you want to swap between in that component.
You can of course just set the same size
and position
to the SpriteComponent
instead of passing them to the animationComponent2
, but I think you will get other problems after that part is done.
You can see documentation for the SpriteAnimationGroupComponent
here