开发者

NSMutableArray crashing at runtime

I'm getting a crash at runtime when try to access an NSMutable array that is correctly loaded. here is the code

NSMutableArray *gameItems;

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

         //initialize array       
        gameItems = [NSMutableArray array];

        for(int i = 0; i < 3; i++)
        {
            GI *gameItem = [[GI alloc] init];
            gameItem.image = [[CCSprite alloc] initWithFile:@"triangle.png"];
            gameItem.Position = ccp(140+40*i,200);
            开发者_StackOverflow社区[gameItems addObject:gameItem];
            [gameItem release];
            NSLog(@"%d",[gameItems count]); //SHOWS THE SIZE OF THE ARRAY INCREMENTING CORRECTLY
        }
        NSLog(@"%d",[gameItems count]); //show " 3 " correct !

        for(GI *gameItem in gameItems)
        {
            [self addChild:gameItem.image]; 
             NSLog(@"%d",[gameItems count]);  //show 3 correct !
        }
        [self schedule:@selector(callEveryFrame:)];
    }
    return self;
}

- (void) callEveryFrame:(ccTime)dt
{
    NSLog(@"----->%d",[gameItems count]); //CRASHES AT RUNTIME IN THIS LINE
}
@end

Please somebody explain me why is this happening. Could the autorelease feature of the NSMutableArray be the problem?


(reposted on request)

If your array gameItems is a member, which it seems it is, to be able to access it in other functions such as callEveryFrame then surely you need to have initialised it in this way: gameItems = [[NSMutableArray alloc] init];

(you missed the alloc I reckon)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜