设置QTimer间隔时QT应用程序崩溃
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;
}
得分.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);
}
似乎您正在创建两个类别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
,您可以从包含这样的设置类的任何类中访问(set/get):
#include <settings.h>
Settings::spawnInterval = 100; // set
int value = Settings::spawnInterval; //get
此行:Game * game; game->setSpawnInterval(200)
导致您的程序崩溃:必须初始化游戏指针;例如,为了解决此问题,您可以在分数类中保存游戏的参考(指针),从而让您调用SetSpawnInterval;我将在游戏的构造函数中构建分数将this
作为参数传递;如@aghilpro所建议,这可以使您免于创建新类。实际上,struct
会更好,因为您的信息是公开的,并且可以从其他类中访问,而无需实现Geters/setter。
相关文章:
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 使用 Bazel 编译 QT 应用程序时访问资源
- 将 Crashpad 与 Windows Qt 应用程序集成
- 在Visual Studio中单实例Qt应用程序版本5.11.1中,在所有其他窗口的顶部打开Qt MainWindow
- 在 Ubuntu 服务器上运行 QT 应用程序时崩溃
- 如何在Android设备上解析Qt应用程序的ClassNotFoundEception?
- Qt应用程序找不到第三方DLL并崩溃
- 在 Linux 上部署 Qt 应用程序
- 如何设置Qt应用程序以使用类Unix系统的实际环境变量?
- Android因为权限问题而杀死Qt应用程序
- 初始化和终止Qt应用程序的正确方法?
- Qt 5 应用程序崩溃并出现"qLineEdit::setText"
- 从 ParaView 构建自定义 Qt 应用程序示例时出错
- Qt 应用程序具有意外的 QDir::currentPath
- Qt应用程序不响应鼠标和键盘事件
- 在Qt应用程序中包含Python.h会导致对Qt函数的未定义引用
- 需要有关在qt c ++应用程序中使用崩溃问题的建议
- 从Qt应用程序执行MSI文件
- Qt 应用程序中与Qt版本相关的运行时错误
- 将Qt应用程序与CMake和Visual Studio链接起来