问题描述
我希望我的问题不总是相同的问题。
我有一个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);
}
}
}
}
actor是我的游戏角色(在他的职能,如移动ecc)在我的核心部分的progamm。 sceneItems是我将每个actor关联到他的像素图的地图。 x,y是抽象场景中的位置,x * SIZE是图形中的位置。
我用这种方法动画我的QGraphicItems: / p>
QGraphicsItemAnimation类为QGraphicsItem提供了简单的动画支持。
链接网站的示例:
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 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();
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.
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();
这篇关于使用QGraphicsScene平滑动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!