0

任何人都可以回答,我应该改变精灵图像,我用我的功能来做

-(void) openKeyWithSprite:(id) sender withSpriteName:(NSString*)spriteName

这会导致内存泄漏还是可以?

在初始化

_spriteBonus=[CCSprite spriteWithFile:@"monstr_1_1.png"];

按计划进行

-(void) openKeyWithSprite:(id) sender withSpriteName:(NSString*)spriteName
{
        CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:spriteName];
        [_spriteBonus setTexture: tex];


}
4

2 回答 2

0

If you want to create frame animation, use CCAnimate action. For example,

id yourAnimation = [CCAnimation animationWithFrames: arrayOfFrames];
id animateAction = [CCAnimate actionWithDuration: animationDuration 
                                       animation: yourAnimation
                            restoreOriginalFrame: YES];
[yourSprite runAction: animateAction];

if you need to repeat your animation, use CCRepeat of CCRepeatForever action, for example

id resultAction = [CCRepeatForever actionWithAction: animateAction];
[yourSprite runAction: resultAction];
于 2012-04-17T15:48:25.710 回答
0

您在此处显示的代码应该没问题,并且不会泄漏任何内存。 setTexture释放旧的纹理参考并保留新的。

但是,假设您没有使用 ARC:[CCSprite spriteWithFile:@"monstr_1_1.png"];autorelease池中,因此请确保将其添加到父CCNode级(在退出您创建它的任何函数之前),以便将其保留在内存中。

于 2012-04-17T15:51:41.187 回答