Question

I hope my question isn't always the same question. I have a QGraphicsScene. Its items are some QGraphicsPixmaps. I move them with a timer that every second does SetX +10. I set +10 cause the window is large 100. With this solution my animations aren't smooth. I thought I could make them smoother by setting instead of +10 a +1...+10. But it didn't work.

Can you suggest me a way to do that please? Thanks a lot.

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);
        }
    }
}
}

Each actor is my game character (in his functions, such as moving ecc) in my core part of the progamm. sceneItems is a map where I associate each actor to his pixmap. x, y are the positions in the abstract scene and x*SIZE is the position in the graphicscene. The positions are updating in the abstract scene and i represent them in the graphicscene.

Was it helpful?

Solution

I animated my QGraphicItems with this approach:

"The QGraphicsItemAnimation class provides simple animation support for QGraphicsItem." http://doc.qt.io/qt-4.8/qgraphicsitemanimation.html

Example from the linked site:

 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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top