[英]Reading a 2D array of pointers to a class
我正在设计一个非常简单的视频游戏,以帮助学习使用SDL。 我遇到了碰撞检测的问题。 为了绘制游戏地图(使用图块),我在一个名为Map
的类中创建了一个指向名为Tile
的类的2D数组。 Map
类有一个名为set_tiles
的函数,它通过从.MAP
文件中读取它们来定义所有tile。 我还有一个名为Hero
的类,它包含有关英雄的所有函数和变量。 一切都非常微不足道。 但是,当我尝试从其他任何地方读取数组时,它会产生奇怪的结果(通过错误测试,我发现x和y值有数千个关闭,但我可能在那里犯了错误)。 这是代码。
SDL_Surface* Map::set_tiles ( SDL_Surface* tile_image ) {
Setup setup;
//Make a temporary map to draw the tiles to
Uint32 rmask, gmask, bmask, amask;
if ( SDL_BYTEORDER == SDL_BIG_ENDIAN ) {
rmask = 0x00000000;
gmask = 0x00000000;
bmask = 0x00000000;
amask = 0x00000000;
}
else {
rmask = 0x00000000;
gmask = 0x00000000;
bmask = 0x00000000;
amask = 0x00000000;
}
SDL_Surface* temp_map = SDL_CreateRGBSurface(SDL_SWSURFACE, MAP_WIDTH, MAP_HEIGHT, 32, rmask, gmask, bmask, amask);
//Open the map
std::ifstream map ( "Test.map" );
//Catch any errors
if ( map.fail() ) return NULL;
//Initialize the tiles
for ( int y = 0; y < MAP_HEIGHT / TILE_HEIGHT; y++ ) {
for ( int x = 0; x < MAP_WIDTH / TILE_WIDTH; x++ ) {
//Determines the tile type
int tile_type = -1;
//Read the tile type from the map
map >> tile_type;
//Make sure it's a real tile
if ( tile_type < 0 || tile_type >= TILE_SPRITES ) {
map.close();
return NULL;
}
//Error check for the .map file
if ( map.fail() ) {
map.close();
return NULL;
}
//Add the tile to the array
tile_array[x][y] = &Tile ( x, y, tile_type );
//Create the temp. image crop
SDL_Rect* temp_crop = &tile_array[x][y]->get_crop();
//Edit the temp. map
setup.apply_surface ( x * TILE_WIDTH, y * TILE_HEIGHT, tile_image, temp_map, temp_crop );
}
}
map.close();
//Return the modified map
return temp_map;
}
现在,如果我尝试在其他地方阅读它,我有一个问题。 IE
bool Hero::collision_check ( Map map ) {
for ( int y = 0; y < MAP_HEIGHT / TILE_HEIGHT; y++ ) {
for ( int x = 0; x < MAP_WIDTH / TILE_WIDTH; x++ ) {
Tile* tile = map.tile_array[x][y];
if ( collision ( box, tile->get_box() ) ) {
//Switch the effect based on the tile type
switch ( tile->get_type() ) {
case TILE_RED:
case TILE_GREEN:
case TILE_BLUE:
return false;
break;
case TILE_CENTER:
case TILE_TOP:
case TILE_TOPRIGHT:
case TILE_RIGHT:
case TILE_BOTTOMRIGHT:
case TILE_BOTTOM:
case TILE_BOTTOMLEFT:
case TILE_LEFT:
case TILE_TOPLEFT:
return true;
break;
default:
return false;
}
}
}
}
return false;
}
当谈到指针(或大多数其他东西)时,我不是主人。 有没有明显的缺陷可以解释我的问题?
这将是一个超出范围的临时指针:
tile_array[x][y] = &Tile ( x, y, tile_type );
你可能正在寻找
tile_array[x][y] = new Tile ( x, y, tile_type );
PS1:如果顶部的if语句看起来不太有用
PS2:如果你发布一个最小的例子而不是你的所有代码,下次它会有所帮助 - 人们倾向于忽略带有太多代码的帖子来阅读
编辑:
你在做什么基本上是:
Foo* fp;
// a Foo() is created, its address assigned to fp,
// but it is then destroyed at the end of the statement
fp = &Foo(2);
// now fp is a pointer to unallocated memory and using it causes undefined behaviour
fp.val; // this value is now undefined
在线
tile_array[x][y] = &Tile ( x, y, tile_type );
您正在为向量分配临时地址。 一旦表达式结束,临时就会被销毁,你现在保存的指针指向一个无效的对象。 我会将tile对象本身存储在tile_array
,而不是存储指向Tile对象的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.