C++ 克莫维。如何设置循环计数?
我正在使用QMovie和gif创建碰撞后的爆炸。问题是我的gif循环了一次又一次,我检查了循环计数状态,它返回-1(无限)。如何只显示一次我的gifC++ 克莫维。如何设置循环计数?,c++,qt,C++,Qt,我正在使用QMovie和gif创建碰撞后的爆炸。问题是我的gif循环了一次又一次,我检查了循环计数状态,它返回-1(无限)。如何只显示一次我的gif #include "Bullet.h" #include <QTimer> #include <QGraphicsScene> #include <QList> #include "Enemy.h" #include "Game.h" #include <typeinfo> #include "lev
#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;
}
}
#包括“Bullet.h”
#包括
#包括
#包括
#包括“敌人h”
#包括“Game.h”
#包括
#包括“levels.h”
外人游戏*游戏;//有一个外部全局对象称为game
int Bullet::killed=0;
int Bullet::missed=0;
双项目符号::精度=0;
Bullet::Bullet(QGraphicsItem*父项):QGraphicsPixmapItem(父项){
//画图
setPixmap(QPixmap(:/images/res/images/bullets/bullet.png”);
missed++;//创建项目符号时增加missed
电影=新的QMovie(:/images/res/images/effects/explosion/64x48.gif);
processLabel=新的QLabel;
processLabel->setMovie(电影);
//制作/连接计时器,每隔一段时间移动一次子弹
QTimer*定时器=新的QTimer(此);
连接(计时器、信号(超时())、此、插槽(移动());
//启动计时器
定时器->启动(2);
}
void Bullet::move(){
//获取当前与此项目符号冲突的所有项目的列表
QList colling_items=collingitems();
//如果发生碰撞的物品之一是敌人,则摧毁子弹和敌人
对于(int i=0,n=collisting_items.size();i得分->增加();
//播放爆炸动画
电影->开始();
电影->设定速度(180);
processLabel->setAttribute(Qt::WA_NoSystemBackground);
processLabel->setGeometry(QRect(x()-15,y()-15,64,48));
scene()->addWidget(processLabel);
qDebug()loopCount();
//连接(电影、信号(已完成())、电影、插槽(停止());
//从场景中删除它们(仍在堆上)
场景()->removeItem(碰撞项[i]);
场景()->removeItem(此);
//从堆中删除它们以节省内存
删除冲突项[i];
删除此项;
杀死++;
未命中--;//如果子弹与敌人碰撞,则说明未命中
如果((终止+1)%9==0)
{
游戏->关卡->关卡::递增关卡();
游戏->得分->得分::addToSum();///TODO
}
//qDebug()我有同样的问题,但什么也没找到,所以我的解决方案如下:
将信号“frameChanged(int)”连接到:
如果要运行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)));
就这样…希望它能帮助你
connect(movie, SIGNAL(frameChanged(int)), this, SLOT(frameChanged_Handler(int)));