cocos2d改变精灵纹理

时间:2013-03-21 11:16:52

标签: objective-c cocos2d-iphone cctexturecache

我想用精灵表中的另一个纹理更改精灵纹理

//normal sprite
character = [CCSprite spriteWithSpriteFrameName:@"char1.png"];

//change texture
[character setTexture:[[CCTextureCache sharedTextureCache] addImage:[CCSprite spriteWithSpriteFrameName:@"char2.png"]]];

但这给了我一个错误。我试过没有spriteWithSpriteFrameName,它给了我一个 空白纹理。

//error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite lastPathComponent]: unrecognized selector sent to instance 0xab32580'

请帮我解决这个问题

2 个答案:

答案 0 :(得分:2)

AddImage采用输入imageName(NSString)而不是sprite(CCSprite)。

[[CCTextureCache sharedTextureCache] addImage:TEX_IMAGE_NAME]; 

这里有一些有用的代码可以帮助您解决崩溃问题:

CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"char2.png"];
[crane setTexture:sprite.texture]; //Already allocated memory? if not thn try below
//crane = [CCSprite spriteWithTexture:sprite.texture];
//crane = [CCSprite spriteWithTexture:sprite.texture rect:rect];

答案 1 :(得分:2)

尝试在CCSprite上设置displayFrame,例如,

[character setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                        spriteFrameByName:@"char2.png"];