c++ - 创建滚动背景(从上到下滚动)

标签 c++

我在创建滚动背景时遇到问题。我实际上是在尝试将我两年前做的 C# 翻译成 C++,作为一个“新手”(可以说是),我遇到了麻烦。

下面是我正在使用的变量和对象。

//ScrollingBackground Inits from the Contructor/Main Method
_screenHeight = Graphics::GetViewportHeight();
_screenWidth = Graphics::GetViewportWidth();

//ScrollingBackground Content from the Load Content Method
_backgroundPosition = new Vector2(_screenWidth / 2, _screenHeight / 2);
_origin = new Vector2(_backgroundTexture->GetHeight() / 2, 0);
_textureSize = new Vector2(0, _backgroundTexture->GetHeight());
_backgroundTexture->Load("background.dds", false);

这是我尝试在发生滚动的地方制作的方法。

void Player::Scrolling(float deltaX)
{
    //This is where the scrolling happens
    _backgroundPosition->X += deltaX;
    _backgroundPosition->X = _backgroundPosition->X % _textureSize->Y;
}

这仍然是相对较新的,所以如果我含糊不清或听起来我不知道我在说什么,请原谅我。

非常感谢,

瑞安。

最佳答案

您不能在 float 上使用 % 运算符。以下解决了您的问题,但不会给出真正的余数。如果精度不是问题,您可以使用以下代码,滚动背景不会出现大问题。

void Player::Scrolling(float deltaX)
{
    //This is where the scrolling happens
    _backgroundPosition->X += deltaX;
    _backgroundPosition->X = static_cast<int>(_backgroundPosition->X) % static_cast<int>(_textureSize->Y);
}

关于c++ - 创建滚动背景(从上到下滚动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27001269/

相关文章:

c++ - 如何将 CryptoPP::Integer 转换为 char*

c++ - 为什么 `std::unordered_map` "speak like the Yoda"- 重新排列元素?

c++ - 什么是复制省略和返回值优化?

c++ - 循环中的前增量和后增量有什么区别(for/while)?

c++ - long long 转换为字节数组

c++ - 将字节连接到字符串

c++ - 我在 myprogramminglab 中收到错误 "stoi is not a member of std"

C++ 从函数数组中调用函数

c++ - 层次结构中的对象类型检测

c++ - OpenCV:快速量化两幅图像之间的差异