C ++混合对象数组

时间:2013-04-22 19:11:07

标签: c++ arrays object

我正在阅读http://www.tonypa.pri.ee/tbw/tut02.html上的磁贴引擎教程,但是当我将Flash代码翻译成C ++时,我陷入困境。到目前为止,我理解并翻译了这一部分:

unsigned int MAPDATA[6][8] = {
    { 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 1, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 1, 1, 1, 1, 1, 1, 1 }
};

class Tile0 {
    public:
        Tile0();
        bool walkable = true;
        unsigned int frame = 0;
};

class Tile1 {
    public:
        Tile1();
        bool walkable = false;
        unsigned int frame = 1;
};

但在这条线上,它说,

  

“第一行游戏.Tile0 = function(){}声明新的对象原型。当我们从地图数组中得到0时,我们将使用Tile0作为模板在该位置上创建新的tile对象。”

是否意味着将数组MAPDATA转换为对象数组?

unsigned int MAPOBJS[6][8] = {
    { Tile1, Tile1, Tile1, Tile1, Tile1, Tile1, Tile1, Tile1 },
    { Tile1, Tile0, Tile0, Tile0, Tile0, Tile0, Tile0, Tile1 },
    { Tile1, Tile0, Tile1, Tile0, Tile0, Tile0, Tile0, Tile1 },
    { Tile1, Tile0, Tile0, Tile0, Tile0, Tile1, Tile0, Tile1 },
    { Tile1, Tile0, Tile0, Tile0, Tile0, Tile0, Tile0, Tile1 },
    { Tile1, Tile1, Tile1, Tile1, Tile1, Tile1, Tile1, Tile1 }
};

或者它是否意味着与Tile0间接引用0?你如何在C ++上做到这一点,以后你可以......

if ( MAPOBJS[1][5].walkable )
    player.moveLeft();

用于共谋检测?

1 个答案:

答案 0 :(得分:0)

首先要阅读原创文章..好吧我认为有什么建议可以替代(不要假装优化或任何东西),但我希望它会给你一般的想法。

class Tile {
   public:
     enum {
        TILE0,
        TILE1
     };

     Tile(int type) : m_type(type) {};
   protected:
     int m_type;   
};

class Tile1 : public Tile {
     Tile1( Tile::TILE0 );
};

class Tile2 : public Tile {
     Tile2( Tile::TILE1 );
};

int mapData[6][8] = {
     { Tile::TILE0, Tile::TILE1, ... }, 
     { ... }
};

Tile * mapObject[6][8];


initMap() {
   for (int i=0;i<6;i++) 
     for (int j=0;j<8;j++) {
       switch (mapData[i][j]) {
           case Tile::TILE0:
              mapObect[i][j] = new Tile1();
           break;
           case Tile::TILE1:
              mapObject[i][j] = new Tile2();
           break;
           default:
              mapObject = NULL;
           break;
       }
   }
}