How does CCMenuItemSprite change sprite images?

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.