【问题标题】:Cocos2d iPhone Game Remove menu from sceneCocos2d iPhone Game 从场景中移除菜单
【发布时间】:2011-07-29 16:18:31
【问题描述】:

我使用 Cocos2d iPhone 编写了一些游戏。在我之前的所有游戏中,我都会在设置 CCMenu 时更改场景,然后在完成后离开该场景。在我当前的项目中,我需要菜单存在于当前场景中,并且能够多次打开然后关闭菜单。出于某种原因,我似乎无法理解,removeChild 不会删除菜单。我在网上看到了几个使用 removeChild 的示例,但它对我不起作用。下面是我的菜单代码,当按下 Start/CreateNewAccount 按钮时,我希望将当前菜单完全从场景中删除。

这是在我的 init 方法中。

     CCMenuItemImage *Start = [CCMenuItemImage itemFromNormalImage:@"MakeLemonade.png" selectedImage:@"MakeLemonadePressed.png"
                                                            target:self
                                                       selector:@selector(CreateNewAccount:)];
     CCMenuItemImage *About = [CCMenuItemImage itemFromNormalImage:@"About.png" selectedImage:@"AboutPressed.png"
                                                            target:self
                                                            selector:@selector(About:)];
     Start.position = ccp(-175, -90);
     About.position = ccp(175, -90);

     CCMenu *MainMenu = [CCMenu menuWithItems: Start, About, nil];
    [Start runAction:[CCFadeIn actionWithDuration:1.0]];
    [About runAction:[CCFadeIn actionWithDuration:1.0]];
     [self addChild:MainMenu z:6];

}
return self;
}
-(void) BeginMenuLayer {

//this is not working


[self removeChild:MainMenu cleanup:YES];

}

【问题讨论】:

    标签: iphone objective-c xcode cocos2d-iphone


    【解决方案1】:

    在您的 init 方法中,您已将 MainMenu 声明为局部变量。您没有将其设置为属性,因此您以后去删除它时没有参考。

    1) 确保你有一个像这样声明的属性:

    @property (nonatomic, retain) CCMenu *MainMenu;
    

    2) 在实现的顶部综合它:

    @synthesize MainMenu;
    

    3) 确保在你的 dealloc 中释放它:

    -(void)dealloc {
        self.MainMenu = nil;
        [super dealloc];
    }
    

    4) 构造它时,将其分配给您的属性而不是局部变量:

    self.MainMenu = [CCMenu menuWithItems: Start, About, nil];
    

    现在您有一个对该对象的保留引用,您可以稍后将其传递给removeChild:cleanup:

    【讨论】:

    • 您不需要将其设置为属性。一个 iVar 就足够了。只是想我会澄清这一点。
    • 这是真的。当你进行 ivar 分配时,它只需要额外的努力来避免内存泄漏。
    • 是的,或者您可以为其分配一个标签并删除ChildByTag。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 2023-03-15
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多