我在创建滚动背景时遇到问题。我实际上是在尝试将我两年前做的 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/