2017-09-30 1 views
1

이것이 제가 사이트에 가입 한 첫 번째 질문입니다. Qt 5.9를 사용하여 게임을 개발 중이며 QTimer를 사용하여 화면에 적을 생성합니다. 타이머의 타임 아웃 기능이 호출 될 때마다 적을 스폰합니다. 내가하려고하는 것은 플레이어가 10 명의 적을 말하면 타이머 간격이 줄어들어 적을 더 자주 스폰하여 게임을 조금 더 어렵게 만드는 것입니다. 처음으로 타이머 간격을 설정하면 게임이 완벽하게 실행되지만 두 번째로 setInterval() 메서드가 호출 될 때 플레이어가 10 명의 적을 죽이면 갑자기 충돌이 발생합니다. 나는 그것이 무엇을 일으키는 지 알아 내기 위해 디버깅을 시도했는데, spawnInterval을 설정하려고하면 충돌이 발생하는 것 같습니다. 코딩에 대해 상당히 익숙해 져서 조언이 만족 스럽습니다! 여기 내 코드에서 소스 파일과 코드 관련 같습니다QTimer 간격을 설정할 때 Qt 응용 프로그램 충돌이 발생합니다.

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 
,363,210

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

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

} 
+0

게임이 초기화되지 않았습니다. –

+0

"Game * game"대신 "Game * game = new Game()"을 의미합니까? 나는 그것을 시도했지만 새로운 창을 만들고 그 창에서 게임이 다시 시작됩니다. – Bencsizy

+0

@Bencsizy 타이머를 시작하지 않고도 시간을 변경할 수 있습니까? timer1-> setInterval (getSpawnInterval());'시간 간격을 변경하는 것이 문제가 아닌지 확인하고 싶습니다. – aghilpro

답변

0

당신이 클래스 game의 두 인스턴스를 생성하는 것 같다

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

정적 변수를 사용하여 다중 클래스에서 액세스하는 것이 좋습니다.

프로젝트에이 클래스를 추가

#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 

#include "settings.h" 

int Settings::spawnInterval = 1000; 

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

} 

.H 지금 우리는 정적 변수 이름 spawnInterval

cpp를, 당신이 그것을 액세스 할 수 있습니다 (설정/취득)와 같은 설정 클래스를 포함하는 모든 클래스에서 가져올 수 있습니다.

#include <settings.h> 

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

이 줄은 Game * game; game->setSpawnInterval(200)이므로 프로그램이 중단됩니다. 게임 포인터를 초기화해야합니다. 이것을 고치려면, 예를 들어, Score 클래스 안에서 게임의 레퍼런스 (포인터)를 잡고, setSpawnInterval을 호출 할 수 있습니다; 게임의 생성자 내에서 점수를 구성하여 this을 매개 변수로 전달합니다. @aghilpro가 제안한 것처럼 새로운 클래스를 만들지 않아도됩니다. 실제로 struct은 getter/setter를 구현할 필요없이 정보가 공개되어 다른 클래스에서 액세스 할 수 있기 때문에 더 좋을 것입니다.

관련 문제