Sprite组件在火焰抖动中退出屏幕



我是一个全新的火焰和游戏开发,我知道用相机和视点你可以控制组件,但我的组件退出屏幕,我不知道如何阻止它向前移动,当它到达屏幕末端。

知道怎么做吗?

你不想控制你的SpriteComponent如何随着相机移动,相机会移动你在"世界"中看到的东西。而你想在世界上移动你的组件

如果你不移动相机,那么解决方案可以是通过使用ScreenHitboxCollisionDetection系统来查看组件是否仍然在屏幕内。(你也可以根据屏幕大小来检查边界框)。

像这样的东西,它只是移动到它与屏幕碰撞之前的位置,应该工作:

class YourGame extends FlameGame with HasCollisionDetection {
Future<void> onLoad async {
add(ScreenHitbox());
}
...
}
class YourComponent extends SpriteComponent {
final Vector2 lastPosition = Vector2.zero();
@override
Future<void> onLoad() async {
...
// Adds a hitbox that covers the size of your component,
// so make sure that you have the size set.
add(RectangleHitbox());
}
...
@override
void update(double dt) {
lastPosition.setFrom(position);
// Update your position etc here
}
@override
void onBeginCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
position = lastPosition;
}
}

最新更新