现在的位置: 首页 > 综合 > 正文

CCMenuItemSprite 如何更改sprite 图片

2013年03月13日 ⁄ 综合 ⁄ 共 1190字 ⁄ 字号 评论关闭
============================================================
博文原创,转载请声明出处
电子咖啡(原id蓝岩)
============================================================

在cocos2d的游戏设置见面中,要添加控制声音开关的按钮,我的button使用CCMenuItemSprite来时显得,当开启声音时候,显示A图片,关闭时候显示B图片,这就需要修改CCMenuItemSprite的图片,方法如下:

在创建menu的时候,这只一种图片

     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:)];

-----

注意,在创建CCMenuItemSprite的时候,必须创建你两个CCSprite,否则会出现一下错误:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'child already added. It can't be added again'

----

点击按钮,会触发 audio:函数,此函数定义如下:

-(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;
}

这里我们拿到的sender是CCMenuItemSprite,转换后可以设置其normalImage,selectedImage来实现图片的修改。

其实normalImage并不是严格意义上的图片,而是  CCNode<CCRGBAProtocol>的子类,所以我们可以将其设置为CCSprite。

抱歉!评论已关闭.