在鼠标位置移动精灵



我正在尝试将精灵移至鼠标的位置,而我得到的工作是很紧张的结果

这是我的主要:

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秒钟。

您的抖动很可能是混合绘画和游戏材料的效果。首先修复。

最新更新