我用spriteWithFile创建了一系列的精灵(CCSprite),在运行环境中,我如何替换精灵图片?我需要有规律的替换几个精灵图片。 原问题:Changing the image of a CCSprite
答:Van Do (最佳答案) CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:@"new_image_name"]; [spriteWant2Change setTexture: tex]; 答:Clever 如果你用的是SpriteSheets,下列方法会有效: NSString* newSprite = [NSString stringWithString:@"SPRITE_NAME.png"]; CCSpriteFrameCache* cache = [CCSpriteFrameCache sharedSpriteFrameCache]; [sprite setDisplayFrame:[cache spriteFrameByName:newSprite]]; 答:Zeeshan [yourSprite setTexture:[[CCSprite spriteWithFile:@"yourImage.png"]texture]]; 答:Gaurav 使用SpriteSheets,我建议可以用如下方法: CCSpriteBatchNode *batch; [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFrameWithFile:@"file.plist"]; batch = [[[CCSpriteBatchNode alloc] initWithFile:@"file.png"] capacity:50] autorelease]; [self addChild:batch]; 答:mostafa88 我用C++编码cocos2d-x,如果你选择用spriteSheet,而不是一个单独文件的话,你应该用以下方法,假设,你要在main layer加入spriteSheet: CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sprite_sheet.plist"); _gameBatchNode = CCSpriteBatchNode::create("sprite_sheet.png", 200); this->addChild(_gameBatchNode, kMiddleground); 这里的kMiddleground只是一个被定义的整体。 然后,我准备在现有的图片中,替换掉名为“cloud.png”的精灵图片,我使用如下代码: cloud->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("blackCloud.png") );
答:Van Do
(最佳答案)
答:Clever
如果你用的是SpriteSheets,下列方法会有效:
答:Zeeshan
答:Gaurav
使用SpriteSheets,我建议可以用如下方法:
答:mostafa88
我用C++编码cocos2d-x,如果你选择用spriteSheet,而不是一个单独文件的话,你应该用以下方法,假设,你要在main layer加入spriteSheet:
这里的kMiddleground只是一个被定义的整体。
然后,我准备在现有的图片中,替换掉名为“cloud.png”的精灵图片,我使用如下代码: