How to use Spritesheet? Is possible?

How to use Spritesheet? Is possible?
0.0 0



How i can use spritesheet? Im searching on google if is possible use spritesheet and create animation using spritesheet.




Please refer to the source code in tests/SpriteTest/SpriteTest.cpp SpriteFrameTest::onEnter()


You can use SpriteBatchNode to run animations.

CCSpriteBatchNode *spriteSheet = CCSpriteBatchNode::batchNodeWithFile("sheet.png"); CCSprite *sprite = CCSprite::spriteWithBatchNode(spriteSheet, CCRectMake(0, 0, 45, 40)); spriteSheet->addChild(sprite, 2); //Create an Animation CCAnimation *moveAnimation = CCAnimation::animation(); moveAnimation->setDelay(0.05f); for (int i=0; i<4; i++) { //Add frames of animation //CCRectMake to define the width of each frame. CCSpriteFrame *frame = CCSpriteFrame::frameWithTexture(spriteSheet->getTexture(), CCRectMake(i*50, 0, 45, 40)); moveAnimation->addFrame(frame); } //Running the animation CCAnimate *moveAnimate = CCAnimate::actionWithAnimation(moveAnimation, true); CCRepeatForever *repeatAction = CCRepeatForever::actionWithAction(moveAnimate); sprite->runAction(repeatAction); //Adding spritesheet to the node. this->addChild(spriteSheet, 2);

The Cocos2d-x Programmers Guide


very nice. it works.

but whats the best way to do two animations from the same sprite sheet?

example: the spritesheet has stand and walk animation.



I think that it not work on Cocos2d-X last version more.

How we can use spritesheet on last version of Cocos2d-X?