【问题标题】:Cocos2d removeChild not workingCocos2d removeChild 不工作
【发布时间】:2011-12-11 13:07:28
【问题描述】:
// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {
        buttonPressed = NO;

        CCMenuItem *myMenuItem = [CCMenuItemImage itemFromNormalImage:@"Icon-72.png" selectedImage:@"Icon-Small.png"target:self selector:@selector(menuSelector:)];
        CCMenu *myMenu = [CCMenu menuWithItems:myMenuItem, nil];
        myMenu.position = ccp(50, 50);
        // add the label as a child to this Layer
        [self addChild: myMenu];
    }
    return self;
}

-(void)menuSelector:(id)sender{
    CCSprite *mySprite = [CCSprite spriteWithFile:@"Icon.png"];
    mySprite.position = ccp(100, 100);
    if (!buttonPressed) {
        buttonPressed = YES;
        [self addChild:mySprite];

    }
    else{
        [self removeChild:mySprite cleanup:YES];
        buttonPressed = NO;
    }

}

为什么 removeChild:mySprite 不起作用?我第二次按下按钮(myMenuItem)后,mySprite 仍然存在。请帮我。谢谢。

【问题讨论】:

    标签: iphone objective-c xcode cocos2d-iphone


    【解决方案1】:

    每次您触摸按钮时,您都会创建一个新的精灵实例。您最好将其创建并添加为类变量,以便您可以在所有方法中访问它,然后根据需要将其设置为打开或关闭。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 2013-06-02
      相关资源
      最近更新 更多