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

C++ 创建滚动背景(从上到下滚动),c++,C++,我在创建滚动背景时遇到问题。我真的试着把两年前的C语言翻译成C++,作为一个“Neb”,我遇到了麻烦。p> 下面是我正在使用的变量和对象 //ScrollingBackground Inits from the Contructor/Main Method _screenHeight = Graphics::GetViewportHeight(); _screenWidth = Graphics::GetViewportWidth(); //ScrollingBackground Conten

我在创建滚动背景时遇到问题。我真的试着把两年前的C语言翻译成C++,作为一个“Neb”,我遇到了麻烦。p> 下面是我正在使用的变量和对象

//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;
}
这还是比较新的,所以请原谅我,如果我含糊不清或听起来好像我不知道我在说什么

非常感谢,


Ryan。

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

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

我一直习惯于不解释到底出了什么问题,哈哈,我的道歉_背景位置->X=\U背景位置->X%\U纹理化->Y;我一直得到“%”:非法,左操作数的类型为“float”,右操作数的类型为“float”。谢谢您的输入。这返回了4个错误,“语法错误:标识符_backgroundPosition”“语法错误:;”,intellisense在第1、1、45和86列分别应为a和“>”。它在十进制类型上工作没有意义。模数返回除法的余数。两个小数除以后的余数是多少。获得小数点的真正余数的唯一方法是找到一种方法,将_backgroundPosition->X和_textureSize->Y设置为整数。例如,如果_backgroundPosition->X为3.5,而_textureSize->Y为4.2,则需要将两者都乘以10才能得到整数。->这里他们解释了为什么%不支持十进制类型。老实说,这是有道理的:-这是一个不错的小链接,你知道。非常感谢:-