2011-12-16 318 views
4

我希望我的问题不总是相同的问题。 我有一个QGraphicsScene。其项目是一些QGraphicsPixmaps。 我用一个定时器来移动它们,每一秒都执行SetX +10。 我设置了+10导致窗口很大100. 有了这个解决方案,我的动画并不流畅。我想我可以通过设置而不是+10 a +1 ... + 10来使它们更平滑。但它没有奏效。使用QGraphicsScene平滑动画

你能建议我一种方法吗? 非常感谢。

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)在progamm我的核心部分。 sceneItems是一个映射,我将每个actor与他的pixmap关联起来。 x,y是抽象场景中的位置,x * SIZE是图形中的位置。位置在抽象场景中更新,我代表它们在图形场景中。

+0

Hummm ..代码,请? – karlphillip 2011-12-16 11:27:56

+0

将您的计时器设置为100毫秒的间隔,并且每次都执行+1 – 2011-12-16 11:45:33

回答

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