如何在Cocos2d中卸载图像

时间:2011-02-24 06:32:16

标签: objective-c image cocos2d-iphone instruments

我正在使用cocos2d。在第一次启动时,在第一次Default.png加载时,splash1.png加载为第二次启动。我在仪器中看到,当我替换场景时,内存不会释放。如何从内存中卸载图像?谢谢!

#import "cocos2d.h"
#import "MainMenu.h"

@interface Splash : CCLayer {

    NSMutableArray *m_pSplashes;
    int m_nCurrentSplash;
    CGSize m_szWinSize;
    CCSequence *m_pSequence, *m_pSequenceDefault;
    CCCallFunc *m_pCall;
    CCSprite *m_pSplashDefault, *splash;
    id m_pFadein, m_pDelay, m_pFadeout;
}

@property(nonatomic, retain) CCSequence *m_pSequence;

+(id) scene;
-(void) showNext: (id) sender;

@end

实施档案

#import "Splash.h"

@implementation Splash

@synthesize m_pSequence;

+(id) scene {

    CCScene *scene = [CCScene node];
    Splash *layer = [Splash node];
    [scene addChild: layer];
    return scene;
}   

-(id) init {    
    if( (self=[super init]) ) {     
        m_szWinSize = [[CCDirector sharedDirector] winSize];        
        m_pFadein = [CCFadeIn actionWithDuration:2];
        m_pDelay = [CCDelayTime actionWithDuration:2];
        m_pFadeout = [CCFadeOut actionWithDuration:2];
        m_pCall = [CCCallFunc actionWithTarget:self selector:@selector(showNext:)];

        m_nCurrentSplash = 0;
        m_pSplashes = [[NSMutableArray alloc] init];

        m_pSequenceDefault = [CCSequence actions:m_pFadeout, m_pCall, nil];

        [m_pSplashes addObject:@"splash1.png"];

        m_pSplashDefault = [[[CCSprite alloc] initWithFile:@"Default.png"] autorelease];
        [m_pSplashDefault setRotation:-90];
        [m_pSplashDefault setPosition:ccp(m_szWinSize.width/2, m_szWinSize.height/2)];
        [self addChild:m_pSplashDefault];
        [m_pSplashDefault runAction:m_pSequenceDefault];
        [m_pFadein retain];
        [m_pDelay retain];
        [m_pFadeout retain];
        [m_pCall retain];
    }
    return self;
}


-(void) showNext: (id) sender { 
    if ( m_nCurrentSplash >= [m_pSplashes count] )
    {   
        CCScene *scene = [CCScene node];
        id child = [MainMenu node];
        [scene addChild:child]; 
        [[CCDirector sharedDirector] replaceScene: [CCFadeTransition transitionWithDuration:1 scene:scene]];
        [m_pCall release];
    }
    else
    {
        splash = [[[CCSprite alloc] initWithFile:[m_pSplashes objectAtIndex:m_nCurrentSplash]] autorelease];
        [splash setPosition:ccp(m_szWinSize.width/2, m_szWinSize.height/2)];
        splash.tag = 1;
        [self addChild:splash];
        m_nCurrentSplash ++;
        m_pSequence = [CCSequence actions:m_pFadein, m_pDelay, m_pFadeout, m_pCall, nil];
        [splash runAction:m_pSequence];
    }
}


-(void) dealloc {
    NSLog ( @"dealloc" );
    [m_pSplashes release];
    [m_pFadein release];
    [m_pDelay release];
    [m_pFadeout release];
    [self removeAllChildrenWithCleanup:YES];
    [super dealloc];
}   

@end

1 个答案:

答案 0 :(得分:0)

您可以使用

手动处理
[[CCTextureCache sharedTextureCache]removeTexture:(CCTexture2D *)tex]

或:

[[CCTextureCache sharedTextureCache] removeUnusedTextures];

如果您确定不再使用纹理。