Cocos2d:在数组中添加sprite导致应用程序终止,并发现未捕获的异常

时间:2009-05-21 05:56:31

标签: cocos2d-iphone sprite

当我尝试将sprite添加到数组时,我收到一个未捕获的异常错误,一切看起来都很好..我正在使用MonocleStudios Simplegame项目  monoclestudios dot com / static / simplegame.zip

代码应该是不言自明的:

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 个答案:

答案 0 :(得分:0)

以下工作正常(Cocos2D 0.7.3,如果与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];
}