Reputation: 381
// 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;
}
}
Why is removeChild:mySprite not working? mySprite still there after i pressed the button(myMenuItem) the second time. Please help me. Thanks.
Upvotes: 3
Views: 1443
Reputation: 814
Every time you touch the button you're creating a new instance of the sprite. You're probably better off creating and adding it as a class variable so you can access it in all methods, then just setting it's visibility on or off as needed.
Upvotes: 2