gpt4 book ai didi

C++ 混合对象数组

转载 作者:太空宇宙 更新时间:2023-11-04 14:08:01 25 4
gpt4 key购买 nike

我正在通过 http://www.tonypa.pri.ee/tbw/tut02.html 阅读 tile 引擎教程但是当我将 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;
};

但是在这条线上,它说,

"First line game.Tile0= function () {} declares new object prototype. When we get 0 from map array, we will use Tile0 as template to make new tile object on that position."

是不是说把数组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();

用于合谋检测?

最佳答案

必须先阅读原始文章。嗯,我认为那里提出的建议可以用类似的东西代替(不要假装优化或其他任何东西),但我希望它能给你一个大概的想法。

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

关于C++ 混合对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16154852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com