[英]Qt Application crash when setting QTimer interval
這是我的第一個問題,也是我注冊該網站的原因。 我正在使用Qt 5.9開發游戲,並使用QTimer在屏幕上生成敵人。 每次調用計時器的超時功能時,都會產生一個敵人。 我要嘗試做的是,如果玩家殺死10個敵人,則計時器間隔會縮短,因此敵人會更頻繁地生成,這使游戲更具挑戰性。 第一次設置計時器間隔時,游戲運行完美,但是第二次調用setInterval()
方法時,玩家殺死10個敵人時,游戲突然崩潰。 我嘗試調試它以找出可能的原因,並且當我嘗試設置spawnInterval時似乎崩潰了。 我是編碼的新手,因此請多多指教! 這是我的代碼中的相關源文件和代碼:
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;
}
分數
#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
分數
#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;
}
播放器
#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
播放器
void Player::spawn()
{
Enemy * enemy = new Enemy();
scene()->addItem(enemy);
}
似乎您正在創建類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
這行: Game * game; game->setSpawnInterval(200)
Game * game; game->setSpawnInterval(200)
導致程序崩潰:您必須初始化游戲指針; 要解決此問題,例如,您可以在Score類中保存游戲的參考(指針),從而讓您調用setSpawnInterval;。 我將在Game的構造函數中構造Score,並將this
作為參數傳遞; 如@aghilpro建議的那樣,這可以避免您創建新類。 實際上,一種struct
會更好,因為您的信息是公開的,可以從其他類訪問,而無需實現getter / setter。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.