0

我有一个适用于 mac 的 cocos2d 应用程序,有两个场景。在场景一中,当我按下按钮时,它会通过以下方式加载第二个场景:

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

现在,在第二个 CCScene 中,在 init 方法中,我使用以下方法加载纹理:

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

这使得应用程序在 CCTextureCache.m 上崩溃:276

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

显然 _textures 有 0 个键/值

我不知道为什么会这样,这是一个如此简单的代码!在场景 1 中,我只有一个按钮和一个标签......

它变得更加奇怪,因为如果我改变我的工作流程并首先加载 CompetitiveScene,它会完美运行。

任何想法?

编辑1

如果我执行 CCSprite *player = [CCSprite spriteWithFile:@"bullet.png"];而不是使用 CCSpriteBatchNode,我会遇到完全相同的问题:/

编辑2

如果我执行CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"bullet.png"];,同样的问题

编辑3

这是项目的代码

第一层

//  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

第二层

//  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
4

0 回答 0