tags:

views:

614

answers:

1

Hi, I'm getting an uncaught exception error when I try to add sprites to an array, and everything looks fine to me.. I'm using MonocleStudios Simplegame project for this monoclestudios dot com/static/simplegame.zip

Code should be fairly self-explanatory:

[code]

@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

[/code]

A: 

The following works fine (Cocos2D 0.7.3, if that matters with 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];
}
David Higgins
That works. Thanks David!
Xeno
How do I use these sprites in other functions? If I put "[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(MyFunction) userInfo:nil repeats:YES];"after I declare the sprites, when I try to call the sprites in MyFunction withsprites[0] setPosition: ccp(100,100);I get an undefined error. Where can I define Sprites so I can use it globally? (Or at least as far as the implementation file). I tried putting Sprite *sprites[50]; a couple places in the header file but I would get syntax erros in the implementation file.
Xeno
got it fixed.. easy enough!
Xeno
I used this code myself. To make it public, put the pointer array in the interface, no the implementation.
deeb