【问题标题】:Smooth animations with QGraphicsScene使用 QGraphicsScene 平滑动画
【发布时间】:2011-12-16 11:25:03
【问题描述】:

我希望我的问题并不总是同一个问题。 我有一个 QGraphicsScene。 它的项目是一些 QGraphicsPixmaps。 我用一个每秒做 SetX +10 的计时器来移动它们。 我设置 +10 因为窗口很大 100。 使用此解决方案,我的动画不流畅。我想我可以通过设置而不是 +10 a +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 是图形场景中的位置。位置在抽象场景中更新,我在图形场景中表示它们。

【问题讨论】:

  • 将您的计时器设置为 100 毫秒间隔,并且每次都执行 +1

标签: c++ qt qgraphicsscene


【解决方案1】:

我用这种方法为我的 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();

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多