首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置QTimer间隔时Qt应用程序崩溃

设置QTimer间隔时Qt应用程序崩溃
EN

Stack Overflow用户
提问于 2017-09-30 18:30:19
回答 2查看 723关注 0票数 1

这是我的第一个问题,也是我注册这个网站的原因。我正在开发一个使用QT5.9的游戏,我使用QTimer在屏幕上生成敌人。每次计时器的超时功能被调用时,都会产生敌人。我想做的是,如果一个玩家杀死了10个敌人,时间间隔就会缩短,这样敌人就会更频繁地产卵,从而使游戏更具挑战性。第一次设置定时器间隔时,游戏运行良好,但是第二次调用setInterval()方法时,当玩家杀死10个敌人时,游戏突然崩溃。我试着调试它以找出导致它的原因,而且当我试图设置spawnInterval时,它似乎崩溃了。我是相当新的编码,所以任何建议都是感激的!以下是我代码中的相关源文件和代码:

main.cpp

代码语言:javascript
复制
#include <QApplication>
#include <game.h>

Game * game;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    game = new Game();

    game->show();

    return a.exec();
}

游戏:

代码语言:javascript
复制
#include <QGraphicsScene>
#include <QWidget>
#include <QGraphicsView>
#include "Player.h"
#include "score.h"
#include "Health.h"

class Game: public QGraphicsView{
public:
    Game(QWidget * parent=0);
    QGraphicsScene * scene;
    Player * player;
       Score * score;
       Health * health;
       void setSpawnInterval(int spawnValue);
       int getSpawnInterval();
        void setTimerInterval();
private:
       int spawnInterval = 1000;
};
#endif // GAME_H

game.cpp:

代码语言:javascript
复制
QTimer * timer1 = new QTimer();
QObject::connect(timer1,SIGNAL(timeout()),player,SLOT(spawn()));
timer1->start(getSpawnInterval());
}
void Game::setSpawnInterval(int spawnValue){

 //this is the part where it crashes
spawnInterval = spawnValue;
}

int Game::getSpawnInterval(){
    return spawnInterval;
}

score.h

代码语言:javascript
复制
#ifndef SCORE_H
#define SCORE_H

#include <QGraphicsTextItem>

class Score: public QGraphicsTextItem{
public:
    Score(QGraphicsItem * parent=0);
    void increase();
    int getScore();

private:
    int score;
};
#endif // SCORE_H

score.cpp

代码语言:javascript
复制
#include "score.h"
#include <QFont>
#include "game.h"
#include <QTimer>

void Score::increase()
{
    score++;

    if(score > 3){
    Game * game;
        game->setSpawnInterval(200);}

    //Draw the text to the display
    setPlainText(QString("Score: ") + QString::number(score));

}

int Score::getScore()
{
    return score;
}

player.h

代码语言:javascript
复制
    #ifndef PLAYER_H
#define PLAYER_H

#include <QGraphicsRectItem>
#include <QEvent>
#include <QObject>

class Player: public QObject, public QGraphicsRectItem{
    Q_OBJECT
public:
     Player(QGraphicsItem * parent=0);
   void keyPressEvent(QKeyEvent * event);
    int jumpPhaseNumber = 0;
    bool jumpRun = false;
public slots:
   void spawn();
   void jumpPhase();

};

#endif

player.cpp

代码语言:javascript
复制
void Player::spawn()
{
    Enemy * enemy = new Enemy();
    scene()->addItem(enemy);

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-30 19:10:06

似乎您正在创建类game的两个实例。

我建议您使用静态变量从多个类访问。

将该类添加到项目中:

.cpp

代码语言:javascript
复制
#include "settings.h"

int Settings::spawnInterval = 1000;

Settings::Settings(QObject *parent) : QObject(parent)
{

}

.h

代码语言:javascript
复制
#ifndef SETTINGS_H
#define SETTINGS_H

#include <QObject>
#include <QString>

class Settings : public QObject
{
    Q_OBJECT
public:
    explicit Settings(QObject *parent = 0);

    static int spawnInterval;
};

#endif // SETTINGS_H

现在我们有了一个静态变量名spawnInterval,您可以从包括以下设置类的任何类中访问它(set/get):

代码语言:javascript
复制
#include <settings.h>

Settings::spawnInterval = 100; // set
int value = Settings::spawnInterval; //get
票数 0
EN

Stack Overflow用户

发布于 2017-09-30 19:30:40

这一行:Game * game; game->setSpawnInterval(200)会导致程序崩溃:您必须初始化游戏指针;例如,要修复这个问题,您可以在Score类中保存一个游戏引用(指针),从而可以调用setSpawnInterval;我将在game的构造函数中构造Score,将this作为参数传递;这将避免创建一个新类,正如@aghilpro所建议的那样。实际上,struct会更好,因为您的信息是公开的,并且可以从其他类访问,而不需要实现better /setter。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46505828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档