// change sprite ccsprite pictures in two ways.
// change directly via picture
// using the settexture (cctexture2d*) function, you can reset the texture picture of the Sprite class.
//
auto bg = Sprite::create();
texture2d* texture = Director::getinstance(),Gettexturecache() addimage("Bg2.png");
Bg->settexture(texture);
//
//2, replacing with cached frames
// use the Setdisplayframe (ccspriteframe*) function to replace the sprite picture with the sprite frame.
//
// load plist file to cache
spriteframecache::getinstance(),addspriteframeswithfile("bg_0.plist");
// use Sprite frames to create sprites
Sprite* BGA = Sprite::createwithspriteframename("Bg1.png");
// change Sprite picture
spriteframe* frame = Spriteframecache::getinstance()-Getspriteframebyname ("Bg2.png");
Bga->setspriteframe(frame);
//
/*// loading a synthesized picture
Ccspritebatchnode *spritebatch=ccspritebatchnode::batchnodewithfile ("********.png");
This->addchild (SpriteBatch);
load plist
Ccspriteframecache::sharedspriteframecache ()->addspriteframeswithfile ("Myconfig.plist");
Create Sprite
Ccsprite *headsprite=ccsprite::spritewithspriteframename ("Aaa.png");
when you need to change pictures
Ccspriteframe *frame=ccspriteframecache::sharedspriteframecache ()->spriteframebyname ("Bbb.png");
Headsprite->setdisplayframe (frame);
*/
- first load the decal set
- Ccspritebatchnode *spritebatch=ccspritebatchnode::batchnodewithfile ("Snake.png");
- This ->addchild (SpriteBatch);
- Ccspriteframecache::sharedspriteframecache ()->addspriteframeswithfile ("snake.plist");
- Create Sprite
- Ccsprite *headsprite=ccsprite::spritewithspriteframename ("Headup.png");
- when you need to change pictures
- 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 ();
- Change the picture of My_hero
- My_hero->stopallactions ();
- My_hero->settexture (Hero_hit);
COCOS2DX 3.x (Dynamic change sprite background image)