【发布时间】:2023-02-02 14:54:10
【问题描述】:
我有一个视频游戏,我可以在 4 个方向上移动:上、下、左和右。所以,基本上是“从上面看”。我在游戏中也有墙。如果角色撞到墙上——他不应该穿过它。
我如何在 onCollision/onCollisionStart 中实现碰撞检测,以便角色知道他何时可以向左、向右、向上和向下移动?
我尝试的方法:
@override
void onCollisionStart(
Set<Vector2> intersectionPoints,
PositionComponent other,
) {
super.onCollisionStart(intersectionPoints, other);
if (other is Wall) {
// if character is going up
if (current == MainHeroState.up) {
upCollisionObject = other;
return;
}
// if character is going down
if (current == MainHeroState.down) {
downCollisionObject = other;
return;
}
// ...
}
}
然后在onCollisionEnd:
@override
void onCollisionEnd(PositionComponent other) {
super.onCollisionEnd(intersectionPoints, other);
if (other is Wall) {
// if the link to the object is the same as the link of the collided object
if (other == upCollisionObject) {
upCollisionObject = null;
return;
}
if (other == downCollisionObject) {
downCollisionObject = null;
return;
}
// ...
}
}
但这种做法是不正确的,在很多情况下是行不通的。
【问题讨论】:
-
请提供足够的代码,以便其他人可以更好地理解或重现问题。
标签: flutter dart collision-detection game-development flame