2017-09-30 128 views
1

这是我第一个问题,我登录该网站的原因。我正在开发一款使用Qt 5.9的游戏,并使用QTimer在屏幕上产生敌人。每当定时器的超时功能被调用时,就会产生一个敌人。 我试图做的是如果一个玩家杀了我们说10个敌人,定时器间隔减少,所以敌人会更频繁地产卵,使得游戏更具挑战性。定时器间隔第一次设置时,游戏运行完美,但第二次调用方法时,当玩家杀死10个敌人时,游戏突然崩溃。我试着调试它来找出可能导致它的原因,当我尝试设置spawnInterval时,它似乎崩溃了。 我是相当新的编码,所以任何建议表示赞赏!下面是相关的源文件和代码从我的代码:Qt设置QTimer间隔时应用程序崩溃

的main.cpp

#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(); 
} 

game.h:

#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:

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; 
} 

得分。 h

#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

#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

#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

void Player::spawn() 
{ 
    Enemy * enemy = new Enemy(); 
    scene()->addItem(enemy); 

} 
+0

游戏未初始化。 –

+0

你的意思是:“游戏*游戏=新游戏()”而不是“游戏*游戏”?我试了一下,但它创建了一个新窗口,游戏再次在该窗口中开始。 – Bencsizy

+0

@Bencsizy是否有可能在没有启动计时器的情况下更改超时时间。像这样:''timer1-> start(getSpawnInterval());','timer1-> setInterval(getSpawnInterval());'我想确保更改时间间隔不是问题。 – aghilpro

回答

0

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

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

这个类添加到您的项目:

的.cpp

#include "settings.h" 

int Settings::spawnInterval = 1000; 

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

} 

.H

#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,您可以访问它(设置/获取)从任何类包括设置类如下:

#include <settings.h> 

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

此行:Game * game; game->setSpawnInterval(200)会导致程序崩溃:您必须初始化游戏指针;为了解决这个问题,例如,您可以在Score类中保存游戏的引用(指针),从而让您调用setSpawnInterval;我会在游戏的构造函数中构建Score,传递this作为参数;这可以避免创建一个新类,就像@aghilpro所建议的那样。其实struct会更好,因为您的信息是公开的,并且可以从其他类访问,而无需实施获取者/设置者。

相关问题