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

Creating a scrolling background (Scrolling from top to bottom)

本文关键字:滚动 从上到下 背景 创建      更新时间:2023-10-16

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

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

非常感谢,

莱恩。

不能在浮点数上使用 % 运算符。以下内容修复了您的问题,但不会给出真正的余数。如果精度不是问题,您可以使用以下代码,而不会看到滚动背景的巨大问题。

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);
}