我是cocos2dx的新手,我刚开始写一些小游戏。我已经定义了一个名为Block的类,它继承自CCNode,它包含一个名为m_block的CCSprite类。我通过addObjet()函数将Block对象保存在CCArray m_runningBlock中。
但是,当我尝试从数组中取出CCSprite m_block并通过函数this-> addChilid()将m_block添加到CCLayer时,问题出现了,我无法弄清楚它有什么问题。有人能帮我吗?
class Block :public CCNode
{
public:
int m_x;
int m_y;
CCSprite* m_block;
public:
Block(void);
Block(const char *pszFileName);
~Block(void);
CCSprite* getSprite();
void bindSprite(CCSprite* sprite);
virtual bool init();
CREATE_FUNC(Block);
};
//=====================================
CCSprite* Block::getSprite(){
return this->m_block;
}
void Block::bindSprite(CCSprite* sprite){
this->m_block = sprite;
this->addChild(m_block);
}
bool Block::init(){
return true;
}
//=============================================
CCArray* BlockManager::BlockType(Kind kind){
Block* m_block1 = Block::create();
m_block1->bindSprite(CCSprite::create("Block1.png"));
Block* m_block2 = Block::create();
m_block2->bindSprite(CCSprite::create("Block1.png"));
Block* m_block3 = Block::create();
m_block3->bindSprite(CCSprite::create("Block1.png"));
Block* m_block4 = Block::create();
m_block4->bindSprite(CCSprite::create("Block1.png"));
CCArray* array = new CCArray;
switch(kind){
case A:
m_block1->m_x = 4;
m_block1->m_y = 14;
m_block2->m_x = 5;
m_block2->m_y = 14;
m_block3->m_x = 5;
m_block3->m_y = 13;
m_block4->m_x = 5;
m_block4->m_y = 12;
array->addObject(m_block1);
array->addObject(m_block2);
array->addObject(m_block3);
array->addObject(m_block4);
break;
}
m_block1->release();
m_block2->release();
m_block3->release();
m_block4->release();
return array;
}
//=================================================
bool BlockManager::init()
{
bool bRect=false;
do {
Kind kind = A;
m_runingblock = CCArray::createWithCapacity(5);
m_runingblock->retain();
m_runingblock = BlockType(A);
CCObject* obj=NULL;
Block* block= NULL;
CCSprite* sprite = NULL;
CCARRAY_FOREACH(m_runingblock,obj){
block = (Block*)obj;
sprite = block->getSprite();
sprite->setPosition(ccp(block->m_x*50,block->m_y*50));
this->addChild(sprite);
}
this->scheduleUpdate();
bRect=true;
} while (0);
return bRect;
}
我可以从CCArray获取m_x和m_y但是当我尝试使用addChild()函数时,会发生中断。