2012-04-09 43 views
1

我已經下載了ABC開源並將源代碼轉換爲cocos2d 1.01。 現在我遇到了init Sprite子類的問題。 (我的子類是@interface OrbSprite:CCSprite)在CCSprite子類中調用self.addChild時,iPhone應用程序崩潰?

此代碼是無限循環和較舊的cocos2d版本。

-(id) init { 
self = [super init]; 

if (self) 
{ 
    [self initWithFile:@"bubble1.png"]; 
    Animation *bub = [Animation animationWithName:@"bubble" delay:0 images:@"bubble1.png", @"bubble2.png", nil]; 
    self.bubble = bub; 
    [bub release]; 

    [self addAnimation:bubble]; 

    Label *l = [[Label alloc] initWithString:@"" dimensions:CGSizeMake(45, 45) alignment:UITextAlignmentCenter 
          fontName:@"Arial Rounded MT Bold" fontSize:18]; 
    self.label = l; 
    [l release]; 

} 

return self; } 

我搜索的解決方案可以通過更改init方法的名稱來解決。

-(id) initWithBubbleImage { 

if ((self = [super initWithFile :@"bubble1.png"])) 
{ 
    NSLog(@"OrbSprite init in if self Method"); 

    bubblea = [NSArray arrayWithObjects:@"bubble1.png",@"bubble2.png",nil]; 

bub = [CCAnimation animationWithFrames:bubblea delay:0 ]; 
    [[CCAnimationCache sharedAnimationCache] addAnimation:bub name:@"bubbleAnim"]; 
    self.bubble = bub; 
    [bub release]; 

label = [[CCLabelTTF alloc] initWithString:@"" dimensions:CGSizeMake(45, 45) alignment:UITextAlignmentCenter 
       fontName:@"Arial Rounded MT Bold" fontSize:18]; 
} 
return self; } 

????問題是當更改init名字時,我發現在同一個子類中的「 - (void)setLabelStr:(NSString )str」對於addChild錯誤:在調試區域中。 * * *斷言在 - OrbSprite的addChild:]「和」終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,原因是:「參數必須是非零」「

這是在同一小類方法

- (void) setLabelStr:(NSString *) str { 
[label setString:str]; 
[self addChild:label]; 
[label setAnchorPoint:ccp(0, 13)]; } 

有人幫我請。 ...... 非常感謝。

============================================== =======================

編輯 - 編輯 - 編輯 - 編輯 - 編輯 - 編輯

我已經做了這個回覆。 但仍然不工作 聽的是h和.M碼

OrbSprite.h

這是代碼

#import "cocos2d.h" 
@interface OrbSprite : CCSprite 
{ 
CCLabelTTF *label; 
CCAnimation *bubble; 
BOOL isBubble; 
BOOL isHidden; 
int order; 

CCSequence *popSequence; 
} 
@property (nonatomic, retain) CCLabelTTF *label; 
@property (nonatomic, retain) CCSequence *popSequence; 
@property (nonatomic, retain) CCAnimation *bubble; 
@property (nonatomic, retain) NSArray *bubblea; 
@property (readwrite) BOOL isBubble; 
@property (readwrite) BOOL isHidden; 
@property (readwrite) int order; 
-(id)initWithBubbleImage; 
- (void) pop; 
- (void) setLabelStr:(NSString *) str; 
- (void) showBubble; 
- (void) reset; 
@end 

OrbSprite.m

這是代碼

#import "OrbSprite.h" 
@implementation OrbSprite 
@synthesize bubble; 
@synthesize label; 
@synthesize isBubble; 
@synthesize isHidden; 
@synthesize popSequence; 
@synthesize order; 
@synthesize bubblea; 

-(id) initWithBubbleImage { 

if ((self = [super initWithFile:@"bubble1.png"])) 
{ 

    bubblea = [NSArray arrayWithObjects:@"bubble1.png",@"bubble2.png",nil]; 

    bubble= [CCAnimation animationWithFrames:bubblea delay:0 ]; 
    [[CCAnimationCache sharedAnimationCache] addAnimation:bub name:@"bubbleAnim"]; 

    label = [[CCLabelTTF alloc] initWithString:@"" dimensions:CGSizeMake(45, 45) 
    alignment:UITextAlignmentCenter fontName:@"Arial Rounded MT Bold" fontSize:18]; 
    } 
return self; 
} 

- (void) pop { 
isBubble = NO; 
popSequence = [CCSequence actions:[CCScaleTo actionWithDuration:.1 scale:.5], 
    [CCScaleTo actionWithDuration:.1 scale:2], [CCCallFunc actionWithTarget:self 
    selector:@selector(finishedPopSequence)], nil]; 
[self runAction:popSequence]; 
} 

- (void) finishedPopSequence { 
self.scale = 1; 
[self setDisplayFrameWithAnimationName:@"bubble" index:0]; } 

- (void) reset { 
self.scale = 1; 
[self setDisplayFrameWithAnimationName:@"bubble" index:0]; } 

- (void)showBubble { 
    isBubble = YES; 
    [self removeChild:label cleanup:NO]; 
    [self setDisplayFrameWithAnimationName:@"bubbleAnim" index:1]; 
    self.scale = .5; 
    id Orbshowscale = [CCScaleTo actionWithDuration:1 scale:1.5]; 
    id Orbshowscale2= [CCScaleTo actionWithDuration:.1 scale:1]; 
    [self runAction:[CCSequence actions:Orbshowscale,Orbshowscale2 , nil]]; } 

- (void) setLabelStr:(NSString *) str { 
[label setString:str]; 
[self addChild:label]; //<<< *** Assertion failure in -[OrbSprite addChild:] 
[label setAnchorPoint:ccp(0, 13)]; } 

@end 

我已經修復過放的問題.. ,但它仍然不」工作..

也發現了同樣的問題是

***斷言失敗 - [OrbSprite addChild:]

***終止應用程序由於未捕獲的異常'NSInternalInconsistencyException',原因:'參數必須是非零'

感謝大家誰已親切...

+0

刪除'[小傢伙釋放]' – tipycalFlow 2012-04-09 04:37:57

+0

感謝您的回覆 我嘗試刪除..但它仍錯誤... 我應該怎麼辦...謝謝你 – chgdev 2012-04-09 05:15:46

回答

1

看起來好像你是過度釋放你的init方法中的變量「bub」。你不通過alloc或create方法調用來創建它,所以你不應該釋放它。

+0

感謝reply..Macro 我嘗試刪除它。 'bubblea = [NSArray arrayWithObjects:@「bubble1.png」,@「bubble2.png」,nil]; bub = [CCAnimation animationWithFrames:bubblea delay:0]; [[CCAnimationCache sharedAnimationCache] addAnimation:bub name:@「bubbleAnim」]; self.bubble = bub; [bub release];' 但它也是錯誤的。 我該怎麼辦...謝謝.... – chgdev 2012-04-09 04:40:28

+0

再一次,刪除[bub release]; – Marco 2012-04-09 06:54:20

0

您已將bub分配到self.bubble,然後您發佈bub。兩者都指向相同的地址。 self.label也是如此。試着這樣說:

-(id) init { 
self = [super init]; 

if (self) 
{ 
[self initWithFile:@"bubble1.png"]; 
Animation *bub = [Animation animationWithName:@"bubble" delay:0 images:@"bubble1.png", @"bubble2.png", nil]; 
self.bubble = bub; 
//[bub release]; <-- This shouldn't be done 

[self addAnimation:bubble]; 

Label *l = [[Label alloc] initWithString:@"" dimensions:CGSizeMake(45, 45) alignment:UITextAlignmentCenter 
         fontName:@"Arial Rounded MT Bold" fontSize:18]; 
self.label = l; 
//[l release]; <-- This shouldn't be done 

} 
return self; } 

編輯 -嘗試這個 -

[self addChild:self.label]; //<<< *** Assertion failure in -[OrbSprite addChild:] 
0

使用的Alloc和發佈後它。