我是cocos-2dx开发的初学者。根据我的游戏玩法,我需要二维数组来存储对象,但在cocos-2dx中没有数据类型可以为我提供这样的功能。 所以现在我计划在另一个CCArray中添加CCArray。如何从中添加,检索和填充数据?
答案 0 :(得分:0)
这对你有用......
// this array will hold arrays :)
cocos2d::CCMutableArray< cocos2d::CCMutableArray< cocos2d::CCObject* >* > *pseudoDoubleDimArray;
void addElements()
{
// create an array called, say.., aRow
cocos2d::CCMutableArray< cocos2d::CCObject* > *aRow = cocos2d::CCMutableArray< cocos2d::CCObject* >::arrayWithObjects( NULL );
// add elements to the array, aRow
for( int j=0;j<5;j++ )
{
// let's say the object is a ccsprite...
cocos2d::CCSprite* sprite1 = cocos2d::CCSprite::spriteWithFile( "yourImage.png" );
sprite1->setTag( j+100 );
aRow->addObject( sprite1 );
}
// now create the other array that will hold the array just created...you could add more rows :)
pseudoDoubleDimArray = cocos2d::CCMutableArray< cocos2d::CCMutableArray< cocos2d::CCObject* >* >::arrayWithObjects( aRow, NULL );
}
void accessElements( )
{
for( int i=0;i<pseudoDoubleDimArray->count( );i++ )
{
printf( "\n For row: %d", i+1 );
cocos2d::CCMutableArray< cocos2d::CCObject* > *row = ( cocos2d::CCMutableArray< cocos2d::CCObject* >* )pseudoDoubleDimArray->getObjectAtIndex( i );
for( int j=0;j<row->count();j++ )
{
cocos2d::CCSprite* sprite1 = ( cocos2d::CCSprite* )row->getObjectAtIndex( j );
printf( "\n Sprite %d tag: %d",j+1, sprite1->getTag( ) );
}
}
}