Currently I use this:
for (int x = 0;x < 20; x++)
{
CCSprite* pSprite = CCSprite::create("Block.png");
CC_BREAK_IF(! pSprite);
pSprite->setPosition(ccp(40*x, 40));
this->addChild(pSprite, 0);
}
But can I create only one ’sprite wich is drawing on more as one places on one layer?
walzer
December 2, 2012, 2:36am
#2
No, you can’t. A sprite have only one position param.
But you can create lots of sprite from one texture, like this
CCTexture2D* texture = CCTextureCache::addImage("Block.png");
for (int x = 0; x < 20; x++)
{
CCSprite* pSprite = CCSprite::createWithTexture(texture, CCRectMake(0,0,40,40));
pSprite->setPosition(ccp(40*x, 40));
this->addChild(pSprite, 0);
}
Or use CCSpriteBatchNode to optimize it. RECOMMENDED!
CCSpriteBatchNode* BatchNode = CCSpriteBatchNode::create("Block.png");
for (int x = 0; x < 20; x++)
{
CCSprite* pSprite = CCSprite::createWithTexture(BatchNode->getTexture(), CCRectMake(0,0,40,40));
pSprite->setPosition(ccp(40*x, 40));
BatchNode->addChild(pSprite, 0);
}
this->addChild(BatchNode);
as above, CCSpriteBathNode is the best choose
Try this code :-
//you can write this code in init for cocos2dx
Image img = new Image();
img->initWithImageFile(“bharat_02.png”);
Texture2D tx = new Texture2D();
tx->initWithImage(img);
//and after that
void HelloWorld::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated){
tx->drawAtPoint(Point(100, 100));
tx->drawAtPoint(Point(100, 150));
}
//let me know if it works!!! good luck