Deep Cloning C++ class that inherits CCNode in Cocos2dx

Posted by A Devanney on Game Development See other posts from Game Development or by A Devanney
Published on 2012-10-04T21:24:26Z Indexed on 2012/10/04 21:54 UTC
Read the original article Hit count: 214

Filed under:
|

I stuck with something in Cocos2dx ...

I'm trying to deep clone one of my classes that inherits CCNode.

Basically i have....

GameItem* pTemp = new GameItem(*_actualItem);

// loops through all the blocks in gameitem and updates their position 
pTemp->moveDown(); 

    // if in boundary or collision etc...
if (_gameBoard->isValidMove(pTemp))
{
    _actualItem = pTemp;

    // display the position
    CCLog("pos (1) --- (X : %d,Y : %d)",  _actualItem->getGridX(),_actualItem->getGridY());
}

Then doesn't work, because the gameitem inherits CCNode and has the collection of another class that also inherits CCNode.

its just creating a shallow copy and when you look at children of the gameitem node in the copy, just point to the original?

class GameItem : public CCNode {

// maps to the actual grid position of the shape
CCPoint* _rawPosition;
// tracks the current grid position
int _gridX, _gridY;
// tracks the change if the item has moved
CCPoint _offset;

public:

//constructors

GameItem& operator=(const GameItem& item);
GameItem(Shape shape);

...

}

then in the implementation....

GameItem& GameItem::operator=(const GameItem& item)
{
_gridX = item.getGridX();
_gridY = item.getGridY();
_offset = item.getOffSet();
_rawPosition = item.getRawPosition();

// how do i copy the node?

return *this;
}

// shape contains an array of position for the game character
GameItem::GameItem(Shape shape)
{

_rawPosition = shape.getShapePositions();

//loop through all blocks in position
for (int i = 0; i < 7; i++)
{
    // get the position of the first block in the shape and add to the position of the first block
    int x = (int) (getRawPosition()[i].x + getGridX());
    int y = (int) (getRawPosition()[i].y + getGridY());

    //instantiate a block with the position and type
    Block* block = Block::blockWithFile(x,y,(i+1), shape);

    // add the block to the this node
    this->addChild(block);

}


}

And for clarity here is the block class

class Block : public CCNode{

private:

// using composition over inheritance
CCSprite* _sprite;
// tracks the current grid position
int _gridX, _gridY;
// used to store actual image number
int _blockNo;


public:

Block(void);
Block(int gridX, int gridY, int blockNo);
Block& operator=(const Block& block);


// static constructor for the creation of a block
static Block* blockWithFile(int gridX, int gridY,int blockNo, Shape shape);

...

}

The blocks implementation.....

Block& Block::operator=(const Block& block)
{
_sprite = new CCSprite(*block._sprite);
_gridX = block._gridX;
_gridY = block._gridY;
_blockNo = block._blockNo;

    //again how to clone CCNode?

return *this;
}


Block* Block::blockWithFile(int gridX, int gridY,int blockNo, Shape shape)
{
Block* block = new Block();
if (block && block->initBlockWithFile(gridX, gridY,blockNo, shape))
{
    block->autorelease();
    return block;
}
CC_SAFE_DELETE(block);
return NULL;
}

bool Block::initBlockWithFile(int gridX, int gridY,int blockNo, Shape shape)
{

 setGridX(gridX);
 setGridY(gridY);
 setBlockNo(blockNo);

const char* characterImg = helperFunctions::Format(shape.getFileName(),blockNo);

// add to the spritesheet
CCTexture2D* gameArtTexture = CCTextureCache::sharedTextureCache()->addImage("Character.pvr.ccz");
CCSpriteBatchNode::createWithTexture(gameArtTexture);

// block settings
_sprite = CCSprite::createWithSpriteFrameName(characterImg);


// set the position of the block and add it to the layer
this->setPosition(CONVERTGRIDTOACTUALPOS_X_Y(gridX,gridY));
this->addChild(_sprite);
return true;
}

Any ideas are welcome at this point!!

thanks

© Game Development or respective owner

Related posts about c++

Related posts about cocos2d-x