Cocos 2d: adding Sprite on a second loaded scene crashes
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? Thanks in advanced :)
Nacho
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;
No comments:
Post a Comment