COCOS2DX 3.x (Dynamic change sprite background image)

Source: Internet
Author: User
Tags addchild

// 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);

*/

  1. first load the decal set
  2. Ccspritebatchnode *spritebatch=ccspritebatchnode::batchnodewithfile ("Snake.png");
  3. This    ->addchild (SpriteBatch);
  4. Ccspriteframecache::sharedspriteframecache ()->addspriteframeswithfile ("snake.plist");
  5. Create Sprite
  6. Ccsprite *headsprite=ccsprite::spritewithspriteframename ("Headup.png");
  7. when you need to change pictures
  8. Ccspriteframe *frame=ccspriteframecache::sharedspriteframecache ()->spriteframebyname ("Headleft.png"    );
  9. Headsprite->setdisplayframe (frame);

  1. Ccsprite *my_hero = ccsprite::create ("Hero_a.png");
  2. Cctexture2d *hero_hit;
  3. Ccsprite * temp_obj = ccsprite::create ("Hero_hit.png");
  4. Hero_hit = Temp_obj->gettexture ();
  5. Change the picture of My_hero
  6. My_hero->stopallactions ();
  7. My_hero->settexture (Hero_hit);

COCOS2DX 3.x (Dynamic change sprite background image)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.