1

我已经下载了ABC开源并将源代码转成cocos2d 1.01。现在我遇到了初始化 Sprite 子类的问题。(我的子类是@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;  }

???问题是,当已经更改初始化名称时,我在同一子类中的“ -(void) setLabelStr:(NSString ) str ”中发现 addChild: 在调试区域中出错.. " * * Assertion failure in -[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”而终止应用程序,原因:“参数必须为非零”

感谢所有善良的人...

4

3 回答 3

1

Looks as if you're over-releasing the variable "bub" in your init method. You don't create it via an alloc or create method call, so you shouldn't be releasing it.

于 2012-04-09T04:32:47.733 回答
0

使用 Alloc,然后释放它。

于 2012-04-09T05:31:26.720 回答
0

你已经分配bubself.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 回答