cocos2d for iPhone

A fast, easy to use, free, and community supported 2D game engine

Register or log in - lost password?
  • Blog
  • Store
  • Games
  • Documentation
  • Download
  • About

cocos2d for iPhone » Programming » cocos2d support (graphics engine)

Replace a MenuItemImage

(6 posts) (3 voices)
  • Started 1 year ago by Mik
  • Latest reply from Mik

Tags:

  • CCMenu
  • CCMenuItemToggle
  • music sound replace MenuItemImage mute SimpleAudioEngin
  • mute
  • off
  • on
  • playbackgroundmusic
  • replace menu item
  • SimpleAudioEngine
  • sound
  • stopbackgroundmusic
  • visible
  1. Mik
    Member

    Hi everybody i have this piece of code

    i have this button called botonMuteOn which is kind of a speaker. It mutes the music and i'd like this to be replaced by a new button with the sprite mute2 that plays the music back again
    but something's not working with the removeChild

    any ideas of what's wrong?

    thankyou

    @implementation MainMenuLayer
    - (id) init {
    	self = [super init];
    	if (self != nil) {
    		//music
    		[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"mainSoundtrack.mp3"];
    
    		//mute
    		CCMenuItem *mute = [CCMenuItemImage itemFromNormalImage:@"mute1.png"
    												  selectedImage:@"mute1.png"
    														 target:self
    													   selector:@selector(muteOn:)];
    		botonMuteOn = [CCMenu menuWithItems: mute, nil];
    		botonMuteOn.position =ccp(30, 30);
    		[self addChild: botonMuteOn];
    	}
    	return self;
    }
    
    //functions
    -(void)muteOn:(id)sender {
    	[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
    
    	[MainMenuLayer removeChild:botonMuteOn Cleanup:YES]; 
    
    	CCMenuItem *muteOn = [CCMenuItemImage itemFromNormalImage:@"muteOn.png"
    												selectedImage:@"muteOn.png"
    													   target:self
    													 selector:@selector(muteOff:)];
    	CCMenu *botonMuteOff = [CCMenu menuWithItems: muteOn, nil];
    	botonMuteOff.position =ccp(30, 30);
    	[self addChild: botonMuteOff];
    }
    
    @end
    Posted 1 year ago #
  2. azka
    Member

    Try to [self removeChild:botonMuteOn....]; i wouldnt remove it, i suggest u to make it invisible. botonMuteOn.visible = NO;

    Posted 1 year ago #
  3. Mik
    Member

    It works perfectly with botonMuteOn.visible = NO;
    Thank you azka.

    Here is the code:

    @implementation MainMenuLayer
    - (id) init {
    	self = [super init];
    	if (self != nil) {
    		//music
    		[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"mainSoundtrack.mp3"];
    
    		//muteOn
    		CCMenuItem *muteOff = [CCMenuItemImage itemFromNormalImage:@"mute1.png"
    												  selectedImage:@"mute1.png"
    														 target:self
    													   selector:@selector(muteOn:)];
    		botonMuteOn = [CCMenu menuWithItems: muteOff, nil];
    		botonMuteOn.position =ccp(30, 30);
    		[self addChild: botonMuteOn];
    		botonMuteOn.visible = YES;
    
    		//boton muteOff
    		CCMenuItem *muteOn = [CCMenuItemImage itemFromNormalImage:@"mute2.png"
    													selectedImage:@"mute2.png"
    														   target:self
    														 selector:@selector(muteOff:)];
    		botonMuteOff = [CCMenu menuWithItems: muteOn, nil];
    		botonMuteOff.position =ccp(30, 30);
    		[self addChild: botonMuteOff];
    		botonMuteOff.visible = NO;
    	}
    	return self;
    }
    
    //functions
    -(void)muteOn:(id)sender {
    	[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
    	botonMuteOn.visible = NO;
    	botonMuteOff.visible = YES;
    }
    -(void)muteOff:(id)sender {
    	[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"mainSoundtrack.mp3"];
    	botonMuteOff.visible = NO;
    	botonMuteOn.visible = YES;
    }
    
    @end
    Posted 1 year ago #
  4. azka
    Member

    Im glad to help u:]

    Posted 1 year ago #
  5. cocos0d
    Member

    Here's an alternative approach using CCMenuItemToggle:

    CCMenuItem *soundOn = [[CCMenuItemImage itemFromNormalImage:@"soundOn.png" selectedImage:@"soundOn.png" target:nil selector:nil] retain];
    
    CCMenuItem *soundOff = [[CCMenuItemImage itemFromNormalImage:@"soundOff.png" selectedImage:@"soundOff.png" target:nil selector:nil] retain];
    
    CCMenuItemToggle *soundToggle = [CCMenuItemToggle itemWithTarget:self selector:@selector(soundButton:) items:soundOn, soundOff, nil];
    
    CCMenu *menu = [CCMenu menuWithItems: soundToggle, nil]; //
    
    [self addChild: menu z:1];
    
    - (void) soundButton: (id) sender
    
    {
    
    // turn on/off the sound
    
    }
    Posted 1 year ago #
  6. Mik
    Member

    thankyou very much :D

    Posted 1 year ago #

RSS feed for this topic

Reply

You must log in to post.

cocos2d for iPhone is proudly powered by bbPress.