在鼠标位置移动精灵
moving sprite at mouse location jittery
我正在尝试将精灵移至鼠标的位置,而我得到的工作是很紧张的结果
这是我的主要:
int main() {
Game chessGame;
RenderWindow wnd(VideoMode(400, 400), "Chess Game");
while (wnd.isOpen()) {
Event event;
while (wnd.pollEvent(event))
{
if (event.type == Event::Closed) {
wnd.close();
}
}
wnd.clear();
chessGame.printGame(&wnd);
wnd.display();
}
return 0;
}
printGame()
的重要部分:
// more stuff up there but it's not neccessary
// txt is the texture of the chess piece
Sprite tool(txt);
FloatRect toolRec = tool.getGlobalBounds();
tool.setScale(window->getSize().x / 8 / toolRec.width , window->getSize().y / 8 / toolRec.height);
Mouse m;
Vector2i pos = m.getPosition(*window);
tool.setPosition(pos.x, pos.y);
window->draw(tool); // drawing the sprite
这是一个非常普遍的答案,但是您将不得不更改很多代码以解决您的问题:不要混合渲染和更新代码。
创建一种方法来绘制游戏的当前状态。创建另一种方法来更新游戏的当前状态。
例如,您的Render()
方法呈现棋盘。您的Update()
方法更改了棋子的位置。
为什么两者都没有相同的方法?您想尽可能多地画出游戏,以使您的游戏看起来顺利。您需要高FPS(每秒绘制帧(。但是,您想以绘图速度而是以恒定的速度更新游戏状态。例如,您的棋盘需要2秒钟才能移动。您不希望带有较慢的图形卡的sommody等待更长的时间,或者有人根本看不到计算机,因为动画只花了20毫秒。您希望它在两个系统上需要2秒钟。
您的抖动很可能是混合绘画和游戏材料的效果。首先修复。
相关文章:
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- SFML无法正确移动精灵
- 在鼠标位置移动精灵
- 精灵不在SFML中移动
- 减慢精灵的移动速度
- 将聚焦的精灵逐渐移动到目标位置(C++和SFML)
- 如何顺利移动精灵
- 在 Cocos2DX 中移动动画精灵和路径
- SFML在另一个类中绘制移动精灵
- 当我移动我的精灵时,它一直从窗户掉下来
- 以相同的速度将精灵从一个点移动到另一个点
- 移动精灵和处理事件
- 用VBO和FBO绘制一个移动的精灵
- c++停止敌人精灵相互移动
- 精灵和项目符号不移动C++
- SDL2.0移动精灵与键盘
- C++ DirectX11 2d 游戏:阻止敌人精灵相互移动
- 在矢量中移动精灵
- 防止将一个精灵移动到另一个精灵上