按照示例这里,是否有可能检测到与屏幕特定一侧(例如屏幕底部)的冲突?
颤振版本: 2.2.3
火焰版本:1.0.0-释放
onCollision方法的MyCollidable类
@override
void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
if (other is ScreenCollidable) {
_isWallHit = true;
// Detect which side?
return;
}
}发布于 2021-09-06 09:39:25
这方面没有内置的方法,但是您可以很容易地计算出您是在与使用游戏的大小发生碰撞,并将碰撞点转换为屏幕坐标(如果您不更改缩放级别或移动相机,则无需此步骤)。
代码应该是这样的:
class MyCollidable extends PositionComponent
with Hitbox, Collidable, HasGameRef {
...
void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
if (other is ScreenCollidable) {
_isWallHit = true;
final firstPoint = intersectionPoints.first;
// If you don't move/zoom the camera this step can be skipped
final screenPoint = gameRef.projectVector(firstPoint);
final screenSize = gameRef.size;
if (screenPoint.x == 0) {
// Left wall (or one of the leftmost corners)
} else if (screenPoint.y == 0) {
// Top wall (or one of the upper corners)
} else if (screenPoint.x == screenSize.x) {
// Right wall (or one of the rightmost corners)
} else if (screenPoint.y == screenSize.y) {
// Bottom wall (or one of the bottom corners)
}
return;
}
}https://stackoverflow.com/questions/69067010
复制相似问题