开发者

How to deallocate a CCLayer

开发者 https://www.devze.com 2023-04-11 01:28 出处:网络
I am making a box2d app for the iphone using cocos2d. I am trying to switch my CCLayer going from my HelloWorldLayer to my HomeScene and I get an error \"Thread 1: Program received signal: \"EXC_BAD_A

I am making a box2d app for the iphone using cocos2d. I am trying to switch my CCLayer going from my HelloWorldLayer to my HomeScene and I get an error "Thread 1: Program received signal: "EXC_BAD_ACCESS"." It gives me that error when I try to call [super dealloc] in my dealloc method for my HelloWorldLayer. Please Help. Here is my .h and .mm

@interface HelloWorldLayer : CCLayer
{
    b2World *world;
    Cannon *cannon1;
    Cannon *cannon2;
    Cannonball *cannonball1;
    Cannonball *cannonball2;
    float theMass;
    float theMass2;
    CCSprite *sunBack;
    b2Vec2 cannon1Pos;
    b2Vec2 cannon2Pos;
    CCMenuI开发者_如何学编程temSprite *pauseBut;
    CCMenuItemSprite *playBut;
    CCMenu *pauseMenu;
}
@property(nonatomic)b2Vec2 cannon1Pos;
@property(nonatomic)b2Vec2 cannon2Pos;

@property(nonatomic, retain)CCSprite *sunBack;
@property(nonatomic, retain)Cannon *cannon1;
@property(nonatomic, retain)Cannon *cannon2;
@property(nonatomic, retain)Cannonball *cannonball1;
@property(nonatomic, retain)Cannonball *cannonball2;
@property(nonatomic, retain)CCMenu *pauseMenu;

// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
+(HelloWorldLayer *) sharedLayer;
-(void)createMonkeys;
-(void)restartGame;
-(void)playGame;
-(void)pauseGame;
-(CCSpriteBatchNode*)getSpriteBatch;
-(void)goToHome;

@end

Here is my .mm where I deallocate

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    delete world;
    world = NULL;
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
    [cannon1 removeFromParentAndCleanup:YES];
    [cannon2 removeFromParentAndCleanup:YES];
    [cannonball1 removeFromParentAndCleanup:YES];
    [cannonball2 removeFromParentAndCleanup:YES];

    // don't forget to call "super dealloc"
    [super dealloc];
}


You can try this one instead of the dealloc you have used

-(void) dealloc
{
delete world;
world = NULL;
cannon1 = NULL;
cannon2 = NULL;
cannonball1 = NULL;
cannonball2 = NULL;
[super dealloc];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号