이것이 제가 사이트에 가입 한 첫 번째 질문입니다. 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);
}
게임이 초기화되지 않았습니다. –
"Game * game"대신 "Game * game = new Game()"을 의미합니까? 나는 그것을 시도했지만 새로운 창을 만들고 그 창에서 게임이 다시 시작됩니다. – Bencsizy
@Bencsizy 타이머를 시작하지 않고도 시간을 변경할 수 있습니까? timer1-> setInterval (getSpawnInterval());'시간 간격을 변경하는 것이 문제가 아닌지 확인하고 싶습니다. – aghilpro