QMovie。如何设置循环计数?

时间:2015-12-12 15:07:39

标签: c++ qt

我正在使用QMovie和gif在碰撞后制造爆炸。问题是我的gif一遍又一遍地循环,我检查了loopcount状态,它返回-1(无限)。如何只展示一次我的gif?

#include "Bullet.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include "Enemy.h"
#include "Game.h"
#include <typeinfo>
#include "levels.h"

extern Game * game; // there is an external global object called game
int Bullet::killed = 0;
int Bullet::missed = 0;
double Bullet::accurancy = 0;

Bullet::Bullet(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
    // draw graphics
    setPixmap(QPixmap(":/images/res/images/bullets/bullet.png"));
    missed++; // increse missed when bullet is created

    movie = new QMovie(":/images/res/images/effects/explosion/64x48.gif");
    processLabel = new QLabel;
    processLabel->setMovie(movie);

    // make/connect a timer to move() the bullet every so often
    QTimer * timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(move()));

    // start the timer
    timer->start(2);
}

void Bullet::move(){
    // get a list of all the items currently colliding with this bullet
    QList<QGraphicsItem *> colliding_items = collidingItems();

    // if one of the colliding items is an Enemy, destroy both the bullet and the enemy
    for (int i = 0, n = colliding_items.size(); i < n; ++i){
        if (typeid(*(colliding_items[i])) == typeid(Enemy)){
            // increase the score
            game->score->increase();

            //play explosion animation
            movie->start();
            movie->setSpeed(180);
            processLabel->setAttribute(Qt::WA_NoSystemBackground);
            processLabel->setGeometry(QRect(x()-15,y()-15,64,48));
            scene()->addWidget(processLabel);
            qDebug() << movie->loopCount();
            //connect(movie,SIGNAL(finished()),movie,SLOT(stop()));

            // remove them from the scene (still on the heap)
            scene()->removeItem(colliding_items[i]);
            scene()->removeItem(this);

            // delete them from the heap to save memory
            delete colliding_items[i];
            delete this;

            killed++;
            missed--; // decrese missed if bullet colide with enemy
            if((killed+1) % 9 == 0)
            {
                game->level->Levels::incrementLevels();
                game->score->Score::addToSum(); /// TODO
            }

            //qDebug() << "Already killed: " << killed;
            //qDebug() << "Already missed: " << missed;
            // return (all code below refers to a non existint bullet)
            return;
        }
    }

    // if there was no collision with an Enemy, move the bullet forward
    setPos(x(),y()-1);
    // if the bullet is off the screen, destroy it
    if (pos().y() < 0){
        scene()->removeItem(this);
        delete this;
    }
}

1 个答案:

答案 0 :(得分:3)

我有同样的问题但没有找到任何东西,所以这是我的解决方案:

连接信号&#34; frameChanged(int)&#34;到:

void Bullet::frameChanged_Handler(int frameNumber) {
    if(frameNumber == (movie->frameCount()-1)) {
        movie->stop();
    }
}

如果你想在循环中运行X次,你只需添加一个静态计数器就可以知道你通过最后一帧的次数:

void Bullet::frameChanged_Handler(int frameNumber) {
    static int loopCount = 0;
    if(frameNumber == (movie->frameCount()-1)) {
        loopCount++;
        if(loopCount >= MAX_LOOP_TIMES)
            movie->stop();
    }
}

当然,请与此联系:

connect(movie, SIGNAL(frameChanged(int)), this, SLOT(frameChanged_Handler(int)));

那就是......希望它可以帮助你。