开发者

EXC_BAD_ACCESS on NSMutableArray insertion

I am using an NSMutableArray defined in a CCLayer subclass as follows:

//
//  GameOverScene.h
//  Cocos2DSimpleGame
//
//  Created by Ray Wenderlich on 2/10/10.
//  Copyright 2010 Ray Wenderlich. All rights reserved.
//

#import "cocos2d.h"

@interface scoLayer : CCColorLayer {
    CCLabelTTF *_label;
    CCLabelTTF *_howtoplay;
    NSMutableArray *highscores;

}

@property (nonatomic, retain) CCLabelTTF *label;
@property (nonatomic, retain) NSMutableArray *highscores;
@property (nonatomic, retain) CCLabelTTF *back;
+ (id)initWithScore:(int)lastScore;
+(void)print_label:(int)lb;
+(void)menu;
@end

@interface sco : CCScene {
    scoLayer *_layer;
}

@property (nonatomic, retain) scoLayer *layer;

@end

Here is the .m file for the class:

@implementation sco
@synthesize layer = _layer;

- (id)init {

    if ((self = [super init])) {
        self.layer = [scoLayer node];
        [self addChild:_layer];
    }
    return self;
}

- (void)dealloc {
    [_layer release];
    _layer = nil;
    [super dealloc];
}

@end

@implementation scoLayer
@synthesize label = _label;
@synthesize highscores ; 
//@synthesize how_to_play ;
@synthesize back;
-(id) init
{
    if( (self=[super initWithColor:ccc4(255,255,255,255)] )) {

        CGSize winSize = [[CCDirector sharedDirector] winSize];
        self.label = [CCLabelTTF labelWithString:@"" fontName:@"Arial" fontSize:16];
        //self.how_to_play  = [CCLabelTTF labelWithString:@"" fontName:@"Arial" fontSize:32];

        self.highscores =  [[NSMutableArray alloc] initWithObjects:nil ];

    //  [highscores addObject:@"asdf"]; 
    //  NSLog(@"hig %@", [highscores objectAtIndex:0]);
        _label.color = ccc3(0,0,0);
        _label.position = ccp(winSize.width/2, winSize.height/2);

        [self addChild:_label z:100];

        [self runAction:[CCSequence actions:
                         [CCDelayTime actionWithDuration:3],
            开发者_Go百科             [CCCallFunc actionWithTarget:self selector:@selector(gameOverDone)],
                         nil]];
        CCSprite *bk1 =[CCSprite spriteWithFile:@"bg3.png" ];//rect: CGRectMake(0, 0, 40, 480)];

        [bk1 setPosition:ccp(160, 239)];
        [self addChild:bk1 z:0];

        CCMenuItem *back = [CCMenuItemImage 
                            itemFromNormalImage:@"gameBackButton.png" selectedImage:@"gameBackButton.png" 
                            target:self selector:@selector(back_game:)];

        CCMenu *menu1 = [CCMenu menuWithItems:back,nil];

        menu1.position = ccp(70, 100);
        [menu1 alignItemsVerticallyWithPadding: 40.0f];
        //  [self addChild:menu z: 2];
        [self addChild:menu1 z: 0];


    }   
    return self;
}

In this function:

+(id)initWithScore:(int)lastScore
{
    NSLog(@"score %d", lastScore);

    //NSMutableArray *highscores =  [[NSMutableArray alloc] initWithObjects:nil ];
    //[highscores addObject:@"asdf"];
    if([highscores count] == 0)
}

I want to use the highscores array and insert the data (lastScore), but when I do this the application exits with an EXC_BAD_ACCESS signal. How can I fix this error?


The + before the method declaration indicates, that this is a class method. So you have no access to instance variables.

I think this is more what you want:

-(id)initWithScore:(int)lastScore
{
    NSLog(@"score %d", lastScore);

    if(!self = [super init])
       return nil;

    highscores =  [[NSMutableArray alloc] init];
    [highscores addObject:lastScore];
    return self;
}


You're on the right track, but unless you have many different highscore tables you don't need to make it a separate class, as you've tried to do now. (I.e. 'create many highscore table instances with a class'.) You can of course use a class and create an object of class highscoretable at [[alloc] init] (as yan kun shows), but if all you need is a mutable array to add highscores to in your game, just allocate one in the app delegate, or use other methods for variables shared between viewcontrollers, I think.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜