平滑动画与QGraphicsScene

Smooth animations with QGraphicsScene

本文关键字:QGraphicsScene 动画 平滑      更新时间:2023-10-16

我希望我的问题不总是同样的问题。我有一个QGraphicsScene。它的项是一些QGraphicsPixmaps。我用计时器移动它们,每秒执行SetX +10。我设置+10是因为窗口很大。使用这个解决方案,我的动画并不流畅。我认为我可以通过设置+10而不是+1…+10来使它们更平滑。但这并没有奏效。

你能给我一个建议吗?非常感谢。
void GameGui::updateScene()
{
for (int y = 0; y < game->getHeight(); ++y) {
    for (int x = 0; x < game->getWidth(); ++x) {
        Actor* a = game->get(y, x);
        if (sceneItems[a] != 0) {
        sceneItems[a]->setX(x*SIZE);
        sceneItems[a]->setY(y*SIZE);
        }
    }
}
}

每个演员都是我的游戏角色(在他的功能,如移动ecc)在我的核心部分的程序。sceneItems是一个地图,我将每个演员与他的像素图联系起来。x, y是抽象场景中的位置,x*SIZE是图形场景中的位置。位置在抽象场景中更新,我在图形场景中表示它们

我用这种方法动画了我的QGraphicItems:

" QGraphicsItemAnimation类为qgraphicsitemem提供了简单的动画支持。"http://doc.qt.io/qt - 4.8 -/- qgraphicsitemanimation.html

链接站点示例:

 QGraphicsItem *ball = new QGraphicsEllipseItem(0, 0, 20, 20);
 QTimeLine *timer = new QTimeLine(5000);
 timer->setFrameRange(0, 100);
 QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
 animation->setItem(ball);
 animation->setTimeLine(timer);
 for (int i = 0; i < 200; ++i)
     animation->setPosAt(i / 200.0, QPointF(i, i));
 QGraphicsScene *scene = new QGraphicsScene();
 scene->setSceneRect(0, 0, 250, 250);
 scene->addItem(ball);
 QGraphicsView *view = new QGraphicsView(scene);
 view->show();
 timer->start();