Changing the image of a CCSprite

后端 未结 11 1985
离开以前
离开以前 2020-12-24 12:12

I have created a number of CCSprites using spriteWithFile.

How do I change the image for the sprite during runtime?

I need to change a few sprites images qui

11条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 12:29

    i code cocos2D-x in c++ : when you use spriteSheet instead of using single file, you shoud use this method. suppose you add your spriteSheet in main layer.

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sprite_sheet.plist");
    _gameBatchNode = CCSpriteBatchNode::create("sprite_sheet.png", 200);
    this->addChild(_gameBatchNode, kMiddleground);
    

    kMiddleground is just a defined integer.

    then i want to change picture of a sprite that already has a sprite with name "cloud.png" i use this code to do that:

    cloud->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("blackCloud.png") );
    

提交回复
热议问题