//更換精靈CCSprite的圖片有兩種方式。
//直接通過圖片更換
//使用setTexture(CCTexture2D*)函數,可以重新設置精靈類的紋理圖片。
//
auto bg = Sprite::create();
Texture2D* texture = Director::getInstance()->getTextureCache()->addImage("bg2.png");
bg->setTexture(texture);
//
//2、通過緩存幀替換
//使用setDisplayFrame(CCSpriteFrame*)函數,利用精靈幀更換精靈圖片。
//
//加載plist文件到緩存
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("bg_0.plist");
//使用精靈幀,創建精靈
Sprite* bga = Sprite::createWithSpriteFrameName("bg1.png");
//更換精靈圖片
SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName("bg2.png");
bga->setSpriteFrame(frame);
//
/*//加載合成的圖片
CCSpriteBatchNode *spriteBatch=CCSpriteBatchNode::batchNodeWithFile("********.png");
this->addChild(spriteBatch);
//加載plist
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("myconfig.plist");
// 生成Sprite
CCSprite *headSprite=CCSprite::spriteWithSpriteFrameName("aaa.png");
//需要更換圖片時
CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("bbb.png");
headSprite->setDisplayFrame(frame);
*/
- // 首先載入貼圖集
- CCSpriteBatchNode *spriteBatch=CCSpriteBatchNode::batchNodeWithFile("snake.png");
- this->addChild(spriteBatch);
- CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("snake.plist");
- // 生成Sprite
- CCSprite *headSprite=CCSprite::spriteWithSpriteFrameName("headup.png");
- //需要更換圖片時
- CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("headleft.png");
- headSprite->setDisplayFrame(frame);
- CCSprite *my_hero = CCSprite::create("hero_a.png");
- CCTexture2D *hero_hit;
- CCSprite * temp_obj = CCSprite::create("hero_hit.png");
- hero_hit = temp_obj->getTexture();
- //改變my_hero的圖片
- my_hero->stopAllActions();
- my_hero->setTexture(hero_hit);