Cocos2d: adding sprites in array forces Terminate application with uncaught exception

I am getting uncaught exception error when I try to add sprites to an array and everything looks good to me .. I am using MonocleStudios Simplegame project for this  

The code should be clear enough:

The .m file:

@implementation GameScene

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

        Sprite * bg = [Sprite spriteWithFile:@"game.png"];
        [bg setPosition:ccp(240, 60)];
        [self addChild:bg z:0];
        [self addChild:[GameLayer node] z:1];
        Sprite * bg1 = [Sprite spriteWithFile:@"game.png"];
        [bg1 setPosition:ccp(211, 260)];
        [self addChild:bg1 z:0];
        //the above works fine!
        Sprite * bgX[50];
        //if comment out the next 3 lines, everything runs but I get an "unused variable" warning
        bgX[0] = [Sprite spriteWithFile:@"mytree.png"];
        [bgX[0] setPosition:ccp(240,150)];
        [self addChild:bgX[0] z:0];  
    }
    return self;
}

@end

      

+1


a source to share


1 answer


The following works fine (Cocos2D 0.7.3 if it matters since the 2.1 SDK):



Sprite *sprites[SPRITE_COUNT];
for(int i = 0; i < SPRITE_COUNT; i++) {
    sprites[i] = [Sprite spriteWithFile:@"file.png"];
    [sprites[i] setPosition: ccp(0, 0)];
    [self addChild: sprites[i] z:0];
}

      

0


a source







All Articles