2011-02-24 30 views
0

我正在使用cocos2d。在第一个Default.png加载作为第一个splash时,然后将splash1.png加载为第二个splash。我在乐器中看到,当我替换场景时,记忆不会自由。我如何从内存中卸载图像?谢谢!如何卸载Cocos2d中的图像

#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 

回答

0

要么你手动使用

[[CCTextureCache sharedTextureCache]removeTexture:(CCTexture2D *)tex] 

或处理:

[[CCTextureCache sharedTextureCache] removeUnusedTextures]; 

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

+0

它应该在dealloc上一个场景中使用还是我可以在Init新场景中使用它? – Sveta 2011-02-24 08:09:23

+0

您可以调用“[[CCTextureCache sharedTextureCache] removeUnusedTextures];”任何地方。最好的地方将是,完成使用一些大型图像后的重点。 – 2011-02-24 08:30:57

+0

非常感谢! – Sveta 2011-02-24 08:52:36