2011-02-24 50 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