c++ - 如何创建简单的 2D 隐形传送?

标签 c++ visual-studio-2013 2d vector-graphics direct2d

请帮助我如何实现 Sprite 的简单传送。我只是希望 Sprite 在退出屏幕左侧时平移到屏幕右侧,反之亦然。下面的示例图片..(像吃 bean 人这样的示例游戏,能够从一扇门传送到另一扇门)

enter image description here

我只是想保持简单。不需要困难的算法。如果 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/

相关文章:

c++ - OpenGL 屏幕布局

c++ - Visual Studio 2013 调试器显示 std::string 的奇怪值

visual-studio-2013 - 如何阻止 Visual Studio 2013 将映射源链接添加到 css 文件

c++ - 我无法用 wcout 输出字符串

java - 在二维数组网格中从一个单元格到另一个单元格绘制线条

java - 如何更新代表 map /关卡的字符串;尝试编写一个推箱子游戏

opengl - 具有多种纹理的 VBO

c++ - C++11 类型推断期间的优先级规则是什么?

c++ - 如何识别简单的手写形状?

html - 使用 awesomium 获取页面的 HTML