【发布时间】:2011-10-17 06:53:10
【问题描述】:
我使用 spriteWithFile 创建了许多 CCSprite。
如何在运行时更改精灵的图像?
我需要定期更改一些精灵图像。
【问题讨论】:
标签: cocos2d-iphone
我使用 spriteWithFile 创建了许多 CCSprite。
如何在运行时更改精灵的图像?
我需要定期更改一些精灵图像。
【问题讨论】:
标签: cocos2d-iphone
CCTexture *tex = [CCTexture textureWithFile:fileName];
self.texture = tex;
【讨论】:
如果您使用的是 SpriteSheets,这将起作用。
NSString* newSprite = [NSString stringWithString:@"SPRITE_NAME.png"];
CCSpriteFrameCache* cache = [CCSpriteFrameCache sharedSpriteFrameCache];
[sprite setDisplayFrame:[cache spriteFrameByName:newSprite]];
【讨论】:
[yourSprite setTexture:[[CCSprite spriteWithFile:@"yourImage.png"]texture]];
【讨论】:
在 COCOS2D-X 中你可以通过以下方式做到这一点
CCTexture2D *tex = CCTextureCache::sharedTextureCache()->addImage("xyz.png");
sprit_name->setTexture(tex);
如果你想改变精灵大小,那么也写这行
sprit_name->setTextureRect(CCRectMake(0,0, tex->getContentSize().width, tex->getContentSize().height));
【讨论】:
我在 c++ 中编写 cocos2D-x 代码: 当您使用 spriteSheet 而不是使用单个文件时,您应该使用此方法。 假设你在主层添加你的 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") );
【讨论】:
对于正在使用 Cocos2d-iPhone v 3.0.0 或更高版本的用户
demonSprite.texture = [[CCSprite spriteWithImageNamed:@"steelDemonNormal.png"] texture];
【讨论】:
如果您使用的是 SpriteSheets,那么这将起作用
CCSpriteBatchNode *batch;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFrameWithFile:@"file.plist"];
batch = [[[CCSpriteBatchNode alloc] initWithFile:@"file.png"] capacity:50] autorelease];
[self addChild:batch];
【讨论】:
尝试为您的游戏使用 atlassed 纹理,因为您在许多实例中也使用相同的精灵
使用
CCSpriteFrameCache、CCSpriteBatchNode
因此,您可以在单个纹理中使用更多不同的纹理来优化纹理缓存。并且所有都在单个节点中进行批处理。这也减少了绘制调用的数量并提高了帧速率。
尝试使用
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Characters.plist"];
CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"Characters.png" capacity:10];
[self addChild:batch];
CCSprite* player1 = [CCSprite spriteWithSpriteFrameName:@"Luce.png"];
player1.position = ccp(winSize.width * 0.2, winSize.height * 0.8);
[batch addChild:player1];
并使用
CCSprite* player = nil;
CCARRAY_FOREACH(batch.children, player1) {
// some computational code and condition
if (sonecondition) {
[player1 setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"spriteframename.png"]];
}
}
在更新或代码的其他部分
使用Zwoptex
TexturePacker 创建二维纹理图集
希望对你有帮助
祝你好运
【讨论】:
如果您想连续更改图像,请在您的 init 方法中编写以下代码
CCTexture2D *tex1 = [[CCTextureCache sharedTextureCache] addImage:@"image1.png"];
CCTexture2D *tex2 = [[CCTextureCache sharedTextureCache] addImage:@"image2.png"];
CCTexture2D *tex3 = [[CCTextureCache sharedTextureCache] addImage:@"image3.png"];
CCSprite *sprite = [CCSprite spriteWithTexture:tex1];
//Make above variables as class level to access in whole class
[self addChild:sprite];
//position the sprite according to your need
在你想改变图片的地方写下这一行
[sprite setTexture:tex3];
【讨论】:
你需要以这种方式制作一个 cctexture2d 对象
CCTexture2D* newtexture = [[CCTextureCache sharedTextureCache] addImage:@"new-texture"]; [mainSprite setTexture: newtexture];
如果你想改变精灵图像纹理的运行时间,这是一件需要做的事情。
【讨论】:
您只需要像这样在运行时更改 CCSprite 的纹理。
[aSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"NewImage.png"]];
【讨论】: