如何解除分配 CCLayer

发布于 2024-12-07 18:25:48 字数 1788 浏览 0 评论 0原文

我正在使用 cocos2d 为 iphone 制作一个 box2d 应用程序。我试图将 CCLayer 从 HelloWorldLayer 切换到 HomeScene,但收到错误“线程 1:程序收到信号:“EXC_BAD_ACCESS”。当我尝试在 HelloWorldLayer 的 dealloc 方法中调用 [super dealloc] 时,它给了我这个错误。请帮忙。这是我的 .h 和 .mm

@interface HelloWorldLayer : CCLayer
{
    b2World *world;
    Cannon *cannon1;
    Cannon *cannon2;
    Cannonball *cannonball1;
    Cannonball *cannonball2;
    float theMass;
    float theMass2;
    CCSprite *sunBack;
    b2Vec2 cannon1Pos;
    b2Vec2 cannon2Pos;
    CCMenuItemSprite *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

这是我释放的 .mm

// 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];
}

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;
    CCMenuItemSprite *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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

度的依靠╰つ 2024-12-14 18:25:48

您可以尝试这个而不是您使用过的 dealloc

-(void) dealloc
{
delete world;
world = NULL;
cannon1 = NULL;
cannon2 = NULL;
cannonball1 = NULL;
cannonball2 = NULL;
[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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文