1

ABC オープン ソースをダウンロードし、ソース コードを cocos2d 1.01 に変換しました。現在、init スプライト サブクラスに問題があります。(私のサブクラスは @interface OrbSprite : CCSprite です)

このコードは無限ループで、古いバージョンの 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' が原因でアプリを終了しています。理由: '引数は非 nil でなければなりません'"

これは同じサブクラスのメソッドです

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

誰でも私を助けてください.......どうもありがとうございました。

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

編集 -- 編集 -- 編集 -- 編集 -- 編集 -- 編集

返事としてこうしました。しかし、まだ動作しません Hear's は .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' が原因でアプリを終了します。理由: '引数は非 nil でなければなりません'

優しくしてくださった皆様、ありがとうございました...

4

3 に答える 3

1

init メソッドで変数「bub」を過剰に解放しているように見えます。alloc または create メソッド呼び出しを介して作成しないため、解放しないでください。

于 2012-04-09T04:32:47.733 に答える
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:]
于 2012-04-09T05:20:42.793 に答える
0

Alloc を使用し、その後解放します。

于 2012-04-09T05:31:26.720 に答える