请帮助我如何实现 Sprite 的简单传送。我只是希望 Sprite 在退出屏幕左侧时平移到屏幕右侧,反之亦然。下面的示例图片..(像吃 bean 人这样的示例游戏,能够从一扇门传送到另一扇门)
我只是想保持简单。不需要困难的算法。如果 Sprite 在同一 y 轴向左输入,只需将 Sprite 平移到同一 y 轴即可。这是我尝试过的。
void Physics::Boundary(float PosX, float PosY)
{
this->PosX = PosX;
this->PosY = PosY;
if (this->PosX >= 638.0f)
{
this->PosX = 2.0f;
this->PosY = PosY;
}
if (this->PosX <= 2.0f)
{
this->PosX = 638.0f;
this->PosY = PosY;
}
if (this->PosY >= 638.0f)
{
this->PosX = PosX;
this->PosY = 2.0f;
}
if (this->PosY <= 2.0f)
{
this->PosX = PosX;
this->PosY = 638.0f;
}
}
最佳答案
如果您查看代码的第一个 if 语句,它会设置 PosX
字段至 2.0f
。然而,下一个 if 语句正在检查 PosX <= 2.0f
。这始终是正确的,因为在第一个 if 语句中您将其设置为 2.0f
。在您的情况下,您将始终被“传送”回原始位置( 638.0f
)。您可以尝试使用 if-else 语句来代替:
void Physics::Boundary(float PosX, float PosY)
{
this->PosX = PosX;
this->PosY = PosY;
if (this->PosX >= 638.0f)
{
this->PosX = 2.0f;
this->PosY = PosY;
}
else if (this->PosX <= 2.0f)
{
this->PosX = 638.0f;
this->PosY = PosY;
}
if (this->PosY >= 638.0f)
{
this->PosX = PosX;
this->PosY = 2.0f;
}
else if (this->PosY <= 2.0f)
{
this->PosX = PosX;
this->PosY = 638.0f;
}
}
您也可以只检查小于 2.0f,而不是小于或等于 2.0f。
关于c++ - 如何创建简单的 2D 隐形传送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27409774/