我尝试使用Qt GUI创建平铺的基础地图:我有两个类:一类用于设置从QGraphicPixmapItem继承的平铺图像,一类用于从文本文件加载我的地图,该文本文件是由数字1、0组成的2D数组和2,并将其添加到场景中:但是我的程序意外退出,而且我不知道中断在哪里:
我的瓷砖课:
class mapTile: public QGraphicsPixmapItem
{
public:
enum Tile {DOOR, GRASS, FIRE};
mapTile(): QGraphicsPixmapItem(),currentTile(GRASS){
syncBitmap();
}
Tile getTile() const {return currentTile;}
void setTile(Tile newTile){
if(currentTile!= newTile){
currentTile=newTile;
syncBitmap();
}
}
private:
void syncBitmap() {//Set my image to my tile
switch(currentTile) {
case DOOR:
image->setPixmap(QPixmap(":/mpa/castledoors.png"));
case GRASS:
image->setPixmap(QPixmap(":/map/grass3_blur.jpg"));
case FIRE:
image->setPixmap(QPixmap(":/map/feu1/png"));
}
}
Tile currentTile;
QGraphicsPixmapItem *image;
};
我的班级地图:
Map.h:
class Map
{
public:
static const int TILE_SIZE=20; //value of the tile in pixels!!!->20X20=a tile
void paintMap(QGraphicsScene *scene);
Map();
private:
static const int WIDTH= 13;// width of the grid cell
static const int HEIGHT= 9;//height of the grid cell
//my grid cell map !!!!
int array[WIDTH][HEIGHT];
mapTile *tile; //my tile
};
和Map.cpp
/*Create a default Map*/
Map::Map()
{
QFile myfile("path to my file");
if (!myfile.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::information(0, "error", myfile.errorString());
}
QTextStream in(&myfile);
QString line ;
QStringList fields;
int i=0;
int j=0;
while (!in.atEnd()) {
//Fill my array with my list--> convert Qtring into Int
for (i=0; i<HEIGHT; i++ ) {
line = in.readLine();
fields = line.split(" ");
for(j=0;j<WIDTH;j++)
{
//cout<<fields[j].toInt();
array[i][j] = fields[j].toInt();
}
}
}
}
//Paint my map with my tile
void Map::paintMap(QGraphicsScene *scene){
int i=0, j=0;
tile= new mapTile();
for (i=0; i<HEIGHT; i++){
for(j=0; j<WIDTH; j++){
switch(array[i][j]){
case 0:
tile->setTile(mapTile::GRASS);
tile->setPos(i,j);
scene->addItem(tile);
j+=TILE_SIZE;
case 1:
tile->setTile(mapTile::FIRE);
tile->setPos(i,j);
scene->addItem(tile);
j+=TILE_SIZE;
case 2:
tile->setTile(mapTile::DOOR);
tile->setPos(i,j);
scene->addItem(tile);
j+=TILE_SIZE;
}
i+=TILE_SIZE;//
}
}
}
最后是我的主窗口(我直接给了file.cpp):
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
myMap= new Map;
scene= new QGraphicsScene(this);
myMap->paintMap(scene);
ui->graphicsView->setScene(scene);
}
MainWindow::~MainWindow()
{
delete ui;
}
抱歉,很长的帖子,但我被卡住了!关于我错过的任何想法吗?
好吧,您的代码中至少有五个问题:
myTile
将它们添加到场景时,您不能共享同一实例。每次向场景中添加图块时,都需要创建一个新图块。目前,您仅在开始时创建一个实例Map::paintMap
。
您的myTile
类继承自QGraphicsPixmapItem
,但是它也有一个QGraphicsPixmapItem *image;
未初始化的成员(因此它是漫游指针),但是随后使用它image->setPixmap(QPixmap(":/mpa/castledoors.png"));
会崩溃。相反,您只想调用setPixmap(QPixmap(":/mpa/castledoors.png"))
(调用您的超类的函数)。
在上面的项目中,您可能已经注意到将“ map”拼写错误为“ mpa”,尽管这并不是崩溃的原因。
在mapTile::syncPixmap
和中Map::paintMap
,您都忘记了中的break;
语句switch
。没有这些,所有瓷砖都将看起来像防火瓷砖。
您将瓦片大小添加到i
和j
迭代器中,就好像它们是像素坐标一样,但是同时您将它们用作数组索引。你必须要么使用的像素坐标或多次单独的变量i
,并j
通过TILE_SIZE
调用时setPos
来代替。
祝你好运!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句