0

我是 cocos-2dx 开发的初学者。根据我的游戏玩法,我需要二维数组来存储对象,但是在 cocos-2dx 中没有数据类型可以为我提供这样的功能。所以现在我打算在另一个 CCArray 中添加 CCArray。如何从中添加、检索和填充数据?

4

1 回答 1

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( ) );
        }
    }
}
于 2012-06-28T07:01:18.923 回答