Cocos 2d: adding Sprite on a second loaded scene crashes

223 views Asked by At

I have a cocos2d application for mac, with two scenes. In scene one, when I push a button it loads the second scene by using:

[[CCDirector sharedDirector] replaceScene:[CompetitiveScene node]];

Now, on this second CCScene, in the init method I load a texture using:

CCSpriteBatchNode *parentNodeBullet = [CCSpriteBatchNode batchNodeWithFile:@"bullet.png" capacity:100];

This makes the application crash on CCTextureCache.m : 276

dispatch_sync(_dictQueue, ^{
    tex = [_textures objectForKey: path];
});

Apparently _textures has 0 key/values

I have no idea why is this happening, it's such a simple code! In scene1 I simply have one button and one label ...

It gets more strange because if I change my workflow and load the CompetitiveScene first, it works perfectly..

Any idea?

EDIT1

If I perform CCSprite *player = [CCSprite spriteWithFile:@"bullet.png"]; instead of using the CCSpriteBatchNode, I have the exact same problem :/

EDIT2

If I perform CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"bullet.png"];, same problem

EDIT3

Here is the code of the project

First layer

//  CompetitiveLayer.m

#import "CompetitiveLayer.h"
#import "cocos2d.h"
#import "CollectableIncreaseAmmo.h"

enum {
    kTagParentNode = 1,
};

@interface CompetitiveLayer()
-(void) addNewSpriteAtPosition:(CGPoint)p;
@end

@implementation CompetitiveLayer

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

        CGSize s = [CCDirector sharedDirector].winSize;

        // enable events
        self.mouseEnabled = YES;

        // Use batch node. Faster
        /*
        CCSpriteBatchNode *parentNodeBullet = [CCSpriteBatchNode batchNodeWithFile:@"bullet.png" capacity:10];
        spriteTextureBullet_ = [parentNodeBullet texture];
         */

        NSImage *img = [NSImage imageNamed:@"bullet.png"];

        if(img == nil) // log image is nil
        {
            CCLOG(@"NIL");
        }

        CCSpriteBatchNode *parentNodeBullet = [[[CCSpriteBatchNode batchNodeWithFile:@"bullet.png" capacity:15] retain] autorelease];
        spriteTextureBullet_ = [parentNodeBullet texture];

        [self addChild:parentNodeBullet z:0 tag:kTagParentNode];


        [self addNewSpriteAtPosition:ccp(s.width/2, s.height/2)];
    }
    return self;
}

-(void) addNewSpriteAtPosition:(CGPoint)p
{
    CCLOG(@"Add sprite %0.2f x %02.f",p.x,p.y);

    /*

     CCSprite *player = [CCSprite spriteWithFile:@"bullet.png"];
     player.position = ccp(p.x, p.y);
     [self addChild:player];
     */

    CollectableIncreaseAmmo *sprite2 = [CollectableIncreaseAmmo spriteWithTexture:spriteTextureBullet_];
    [sprite2 setPosition: ccp( p.x, p.y)];
    [self addChild:sprite2];



/*
    CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"bullet.png"];

    CollectableIncreaseAmmo *sprite2 = [CollectableIncreaseAmmo spriteWithTexture:texture];
    [sprite2 setPosition: ccp( p.x, p.y)];
    [self addChild:sprite2];
 */

}

- (BOOL) ccMouseDown:(NSEvent *)event
{
    CGPoint location = [(CCDirectorMac*)[CCDirector sharedDirector] convertEventToGL:event];
    [self addNewSpriteAtPosition: location];

    return YES;
}

- (void) dealloc
{
    [super dealloc];
}

@end

Second layer

//  TitleLayer.m

#import "TitleLayer.h"
#import "CompetitiveScene.h"
#import "cocos2d.h"

@interface TitleLayer()
-(void) createButtons;
-(void) createTitle;
@end

@implementation TitleLayer

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

        // enable events

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
        self.touchEnabled = YES;
        self.accelerometerEnabled = YES;
#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
        self.mouseEnabled = YES;
#endif

        // Create buttons
        [self createButtons];

        // Create title
        [self createTitle];

        //[self scheduleUpdate];
    }
    return self;
}


-(void) createButtons
{
    CCMenuItemLabel *competitiveGameButton = [CCMenuItemFont itemWithString:@"Fight" block:^(id sender){
/*
        CCScene *s = [CCScene node];
        id child = [CompetitiveLayer node];
        [s addChild:child];
        [[CCDirector sharedDirector] replaceScene: s];
 */

//        [[CCDirector sharedDirector] replaceScene: [CCTransitionZoomFlipX transitionWithDuration:1 scene:[CompetitiveScene node] ]];
        [[CCDirector sharedDirector] replaceScene:[CompetitiveScene node]];
        // We can also push and pop the scene
    }];

    CCMenuItemLabel *coperativeGameButton = [CCMenuItemFont itemWithString:@"Coperate" block:^(id sender){
        /*
        CCScene *s = [CCScene node];
        id child = [TitleLayer node];
        [s addChild:child];
        [[CCDirector sharedDirector] replaceScene: s];
         */
    }];


    CCMenu *menu = [CCMenu menuWithItems:competitiveGameButton, coperativeGameButton, nil];
    [menu alignItemsVertically];

    CGSize s = [[CCDirector sharedDirector] winSize];

    menu.position = ccp(s.width/2, s.height/2);

    [self addChild: menu z:-1];

}

-(void) createTitle
{
    CGSize s = [CCDirector sharedDirector].winSize;
     CCLabelTTF *label = [CCLabelTTF labelWithString:@"Select type of game" fontName:@"Marker Felt" fontSize:32];
     [self addChild:label z:0];
     [label setColor:ccc3(0,0,255)];
     label.position = ccp( s.width/2, s.height-50);
}

- (void) update:(ccTime) time {
    CCLOG(@"Testing update");
}

- (void) dealloc
{
    [super dealloc];
}

@end
0

There are 0 answers