gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:18 26 4
gpt4 key购买 nike

这是我的第一个问题,也是我注册该网站的原因。我正在使用 Qt 5.9 开发游戏,并使用 QTimer 在屏幕上生成敌人。每次调用计时器的超时函数时,都会产生一个敌人。我尝试做的是,如果玩家杀死 10 个敌人,计时器间隔会减少,因此敌人会更频繁地产生,使游戏更具挑战性。第一次设置定时器间隔时,游戏运行完美,但第二次调用setInterval()方法时,当玩家杀死10个敌人时,游戏突然崩溃。我尝试调试它以找出可能导致它的原因,当我尝试设置 spawnInterval 时它似乎崩溃了。我对编码还很陌生,所以不胜感激任何建议!以下是我的代码中的相关源文件和代码:

主要.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();
}

游戏.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

游戏.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

分数.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;
}

播放器.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

播放器.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,您可以从任何包含设置类的类访问它(设置/获取),如下所示:

#include <settings.h>

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

关于android - 设置 QTimer 间隔时 Qt 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46505828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com