使用QGraphicsScene平滑动画

时间:2011-12-16 11:25:03

标签: c++ qt qgraphicsscene

我希望我的问题不一定是同一个问题。 我有一个QGraphicsScene。 它的项目是一些QGraphicsPixmaps。 我用一个定时器移动它们,每秒钟做SetX +10。 我设置+10因为窗口大100。 有了这个解决方案,我的动画就不顺畅了。我想我可以通过设置代替+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是一个地图,我将每个actor与他的pixmap相关联。 x,y是抽象场景中的位置,x * SIZE是graphicscene中的位置。位置在抽象场景中更新,我在图形中表示它们。

1 个答案:

答案 0 :(得分:7)

我用这种方法为我的QGraphicItems制作动画:

“QGraphicsItemAnimation类为QGraphicsItem提供简单的动画支持。” 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();