2013-02-20 86 views
0

我正在設計一個非常簡單的視頻遊戲來幫助學習使用SDL。我遇到了碰撞檢測問題。爲了繪製遊戲地圖(使用拼圖),我在一個名爲Map的類中創建了一個名爲Tile的類的指針的二維數組。 Map類具有一個稱爲set_tiles的函數,該函數通過從.MAP文件讀取它們來定義所有圖塊。我還有一個名爲Hero的課程,其中包含有關英雄的所有功能和變量。所有非常微不足道的。但是,當我嘗試從其他任何地方讀取數組時,它會產生奇怪的結果(通過bug測試,我發現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; 

} 

現在,如果我嘗試在其他地方讀取它,我有一個問題。 I.E.

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; 
} 

我不是指針(或大多數其他事物)的主人。是否有任何明顯的缺陷可以解釋我的問題?

回答

1

在你到矢量分配一個臨時的地址線

tile_array[x][y] = &Tile (x, y, tile_type); 

。該表達式一結束,臨時被破壞,現在保存的指針指向一個無效的對象。我不會存儲指向Tile對象的指針,而會將這些圖塊對象本身存儲在tile_array中。

+0

謝謝,我會盡力根據它編輯所有內容。 – 2013-02-20 21:49:33

1

這將是一個指向一個臨時性的即超出範圍:

tile_array[x][y] = &Tile (x, y, tile_type); 

您可能正在尋找

tile_array[x][y] = new Tile (x, y, tile_type); 

PS1:如果在上面的語句似乎不是很有用

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 
+0

請更具描述性,我甚至不明白爲什麼這是一個問題或如何解決它。 – 2013-02-20 21:39:06

+0

第二點建議非常有用,謝謝你。 – 2013-02-20 21:46:40