NSMutableArray插入时的EXC_BAD_ACCESS

时间:2011-02-18 14:25:24

标签: iphone objective-c cocos2d-iphone nsmutablearray exc-bad-access

我正在使用NSMutableArray子类中定义的CCLayer,如下所示:

//
//  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

以下是该类的.m文件:

@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],
                         [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;
}

在此功能中:

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

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

我想使用highscores数组并插入数据(lastScore),但是当我这样做时,应用程序会以EXC_BAD_ACCESS信号退出。我该如何解决这个错误?

2 个答案:

答案 0 :(得分:2)

方法声明前的+表示这是一个类方法。所以你无法访问实例变量。

我认为这更符合您的要求:

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

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

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

答案 1 :(得分:0)

你走在正确的轨道上,但除非你有许多不同的高分表,否则你不需要将它作为一个单独的类,正如你现在所做的那样。 (即'用类'创建许多高分表实例。)你当然可以使用一个类并在[[alloc] init]创建一个类highscoretable的对象(如yan kun所示),但如果你需要的只是一个可变的数组在你的游戏中添加高分,只需在app委托中分配一个,或者在viewcontrollers之间使用其他方法分享变量,我认为。