cocos2dx 3.x(動態改變精靈的背景圖片)


    //更換精靈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);

     

     */

 

 

 

 

  1. // 首先載入貼圖集     
  2. CCSpriteBatchNode *spriteBatch=CCSpriteBatchNode::batchNodeWithFile("snake.png");    
  3. this->addChild(spriteBatch);    
  4. CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("snake.plist");    
  5. // 生成Sprite     
  6. CCSprite *headSprite=CCSprite::spriteWithSpriteFrameName("headup.png");    
  7. //需要更換圖片時     
  8. CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("headleft.png");    
  9. headSprite->setDisplayFrame(frame);

 

 

 

 

  1. CCSprite *my_hero = CCSprite::create("hero_a.png");  
  2.   
  3. CCTexture2D *hero_hit;  
  4.   
  5. CCSprite * temp_obj = CCSprite::create("hero_hit.png");  
  6.   
  7.  hero_hit = temp_obj->getTexture();  
  8.   
  9.   
  10. //改變my_hero的圖片  
  11.  my_hero->stopAllActions();  
  12.   
  13. my_hero->setTexture(hero_hit);  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM