In cocos2d game settings, you need to add a button to control the sound switch. My button appears when CCMenuItemSprite is used. When the sound is enabled, a is displayed, and B is displayed when the image is closed, to modify the image of CCMenuItemSprite, use the following method:
When creating a menu, this image is
[Cpp]
CCSprite * audion = [CCSprite spriteWithSpriteFrameName: @ "button_audio.png"];
CCSprite * audios = [CCSprite spriteWithSpriteFrameName: @ "button_audio.png"];
CCMenuItemSprite * audiosa = [CCMenuItemSprite itemFromNormalSprite: audion selectedSprite: audios target: self selector: @ selector (audio :)];
-----
Note: When creating CCMenuItemSprite, you must create two CCSprite instances. Otherwise, an error occurs:
[Cpp]
Terminating app due to uncaught exception 'nsinternalinconsistencyexception', reason: 'child already added. It can't be added again'
----
Click the button to trigger the audio function. The function is defined as follows:
[Cpp]
-(Void) audio :( id) sender {
NSLog (@ "send: % @", sender );
CCMenuItemSprite * I = (CCMenuItemSprite *) sender;
CCSprite * audion = [CCSprite spriteWithSpriteFrameName: @ "button_audio_bar.png"];
CCSprite * audios = [CCSprite spriteWithSpriteFrameName: @ "button_audio_bar.png"];
I. normalImage = audion;
I. selectedImage = audios;
}
Here we get the sender CCMenuItemSprite. After conversion, we can set its normalImage and selectedImage to modify the image.
In fact, normalImage is not a strictly picture, but a subclass of CCNode <CCRGBAProtocol>, so we can set it to CCSprite.