2011-12-16 13 views
4

私の質問が必ずしも同じ質問ではないことを願っています。 私はQGraphicsSceneを持っています。 そのアイテムはいくつかのQGraphicsPixmapsです。 1秒ごとにSetX + 10を実行するタイマーで移動します。 このソリューションでは、私のアニメーションは滑らかではありません。 +10 a + ... + 10の代わりに+10 aを設定することで、スムーズにすることができると思いました。しかし、それは動作しませんでした。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); 
     } 
    } 
} 
} 

各アクタは、progammの中核部分にある自分のゲームキャラクター(eccを動かすなど)です。 sceneItemsは私が各アクタを彼のpixmapに関連付けるマップです。 x、yは抽象シーン内の位置であり、x * SIZEはgraphicscene内の位置です。位置は抽象的なシーンで更新されており、私はそれらをグラフィックスシーンで表現しています。

+0

Hummm ...コード、してください? – karlphillip

+0

タイマーを100ミリ秒間隔に設定し、毎回+1する –

答えて

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

残念ながら、これはQt 5では時代遅れです。新しい一般的なアニメーションフレームワークで置き換えられました:https://doc.qt.io/qt-5/animation -overview.html –

関連する問題