3
// 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が機能しないのですか? 2回目にボタン(myMenuItem)を押したあと、mySpriteがまだそこにあります。私を助けてください。ありがとう。Cocos2d removeChildが機能しない