在鼠标位置抖动移动精灵
moving sprite at mouse location jittery
我正在尝试将 sprite 移动到鼠标的位置,我得到的效果很好,但它非常不稳定 the result
这是我的主要内容:
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 秒才能移动。您不希望显卡较慢的人等待更长时间,或者计算机速度较快的人根本看不到它,因为动画只用了 20 毫秒。您希望在 两个 系统上花费 2 秒。
您的抖动很可能是混淆绘图和游戏状态的结果。先解决这个问题。
我正在尝试将 sprite 移动到鼠标的位置,我得到的效果很好,但它非常不稳定 the result
这是我的主要内容:
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 秒才能移动。您不希望显卡较慢的人等待更长时间,或者计算机速度较快的人根本看不到它,因为动画只用了 20 毫秒。您希望在 两个 系统上花费 2 秒。
您的抖动很可能是混淆绘图和游戏状态的结果。先解决这个问题。