我的問題是,一些圖層nerver被釋放。我的預覽定罪是,[CCDirector sharedDirector] replaceScene:[Menu node]];或類似爲我做的一切。如果我加載一個新圖層,那麼在後臺保留活動圖層。這導致了怪異的錯誤。無論如何,在onExit調用時,底部的代碼保留計數爲2。 Dealloc永遠不會被調用。刪除rootVC可解決問題。不幸的是我需要它。cocos2d殺死圖層。 replaceScene不調用dealloc。保留計數2
#import "Help.h"
#import "Constants.h"
#import "MainMenu.h"
@implementation Help
-(void)showText:(ccTime)dt
{
[self unschedule:_cmd];
txt.hidden = NO;
}
-(void)onExit
{
CCLOG(@"retain count %i", [self retainCount]);
[super onExit];
}
-(id) init
{
if ((self = [super init]))
{
NSString* bgPath;
NSString* button2Path;
NSString* button2PressPath;
CGRect textFrame;
int fontsize;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
bgPath = @"background-ipad.png";
button2Path = @"button2-ipad.png";
button2PressPath = @"button2_press-ipad.png";
textFrame = CGRectMake(60, 140, 920, 500);
fontsize = 24;
} else {
bgPath = @"background.png";
button2Path = @"button2.png";
button2PressPath = @"button2_press.png";
textFrame = CGRectMake(10, 45, 460, 217);
fontsize = [kMenuFontSize intValue];
}
NSString* helpText = NSLocalizedString(@"Bla bla bla yada yada yada", @"Helptext");
rootVC = [UIApplication sharedApplication].keyWindow.rootViewController; // +1 retain to the layer
txt = [[[UITextView alloc] initWithFrame:textFrame] autorelease];
txt.text = helpText;
[txt setEditable:NO];
[txt setFont:[UIFont fontWithName:@"Sui Generis" size:fontsize]];
[txt setTextColor:[UIColor whiteColor]];
[txt setBackgroundColor:[UIColor clearColor]];
[rootVC.view addSubview:txt];
txt.hidden = YES;
[self schedule:@selector(showText:) interval:1];
self.isTouchEnabled = NO;
CGSize ss = [[CCDirector sharedDirector] winSize];
CCSprite* background = [CCSprite spriteWithFile:bgPath];
background.position = ccp(ss.width*0.5, ss.height*0.5);
[self addChild:background z:0];
CCLabelTTF* backLabel = [CCLabelTTF labelWithString:NSLocalizedString(@"Back", @"back button at help") fontName:kMenuFont fontSize:fontsize];
backLabel.position = ccp(ss.width * 0.1, ss.height * 0.1);
[backLabel setColor:ccBLACK];
[self addChild:backLabel z:20];
CCMenuItemImage* backButton = [CCMenuItemImage itemFromNormalImage:button2Path selectedImage:button2PressPath block:^(id sender) {
[txt removeFromSuperview];
id trans = [CCTransitionFade transitionWithDuration:1 scene:[MainMenu node]];
[[CCDirector sharedDirector] replaceScene:trans];
} ];
backButton.position = ccp(ss.width * 0.1, ss.height * 0.1);
CCMenu* menu = [CCMenu menuWithItems:backButton, nil];
[menu setPosition:ccp(0,0)];
[self addChild:menu z:10];
}
return self;
}
-(void)dealloc
{
CCLOG(@"Help dealloc");
[super dealloc];
}
@end
如何釋放rootVC = [UIApplication sharedApplication] .keyWindow.rootViewController; // +1保留到圖層。 [自我釋放]不起作用。 – zeiteisen 2011-06-01 14:24:28