创建滚动背景(从上到下滚动)
Creating a scrolling background (Scrolling from top to bottom)
我在创建滚动背景时遇到问题。我实际上是在尝试将两年前所做的 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);
}
相关文章:
- 如何在不强制转换每个参数的情况下删除初始值设定项列表中从 int 到 char 的缩小转换?
- 在不损失精度的情况下将double从C++传输到python
- 需要从存档中读取一行文本.txt直到找到"hhh",然后转到下一行
- C++ - 如何从上到下制作 4x4 矩阵和 cout 列(需要解密字符串)
- 如何从上到下翻转 2D 数组?
- Visual C 32位整数从文件到8位字符到文件 - 程序在某些整数上崩溃
- 如何使O(n)的函数检查字母(上和下)和()+-*/到尾递归?
- std::vector 从一行代码清空到下一行代码
- 将字符串[]从C#发送到SafeArrayAccessData()上的C++ Dll 'E_UNEXPECTEDCatastrophic Error'
- 错误:在 DLSYSM 的情况下,从“void*”到“void (*)()”的转换无效
- strcat 3 char从char []到char *在Windows上工作,但在Cygwin中给出了细分故障
- 当孩子存储在基本指针的向量上时,如何从基础到儿童班级动态
- 如何在网格上找到从A到B的最短路径
- C++将字符串字符从下到高,从上到低
- 创建滚动背景(从上到下滚动)
- 从上到下横向打印BST
- 使用方向枚举列表时反转方向(从左到右或从上到下)
- OpenMP:从上到下
- OpenGL ES纹理起源在不同的手机上从上/左到下/左是不同的
- 从上到下,反之亦然,在c++中没有循环