我有一个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个键/值
我不知道为什么会这样,这是一个如此简单的代码!在scene1中,我只有一个按钮和一个标签......
这更奇怪,因为如果我改变我的工作流程并首先加载CompetitiveScene,它就能完美运行..
有什么想法吗?
EDIT1
如果我表演
CCSprite *player = [CCSprite spriteWithFile:@"bullet.png"];
而不是使用CCSpriteBatchNode,我有完全相同的问题:/
EDIT2
如果我执行CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"bullet.png"];
,同样的问题
EDIT3
这是项目的代码
第一层
// 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