cocos2dx CCControlSwitch

CCControlSwitch也是extension中的控件,本身比较简单,直接上例子

// on "init" you need to initialize your instance
bool ControlSwitch01::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !MenuLayer::init() )
    {
        return false;
    }
    
    
    CCSize size = __winSize;
    
    // Create the switch
    CCControlSwitch *switchControl = CCControlSwitch::create
    (
     CCSprite::create("extensions/switch-mask.png"),
     CCSprite::create("extensions/switch-on.png"),
     CCSprite::create("extensions/switch-off.png"),
     CCSprite::create("extensions/switch-thumb.png"),
     CCLabelTTF::create("On", "Arial-BoldMT", 16),
     CCLabelTTF::create("Off", "Arial-BoldMT", 16)
     );
    switchControl->setPosition(ccp (size.width/2, size.height/2));
    addChild(switchControl);
    
    switchControl->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlSwitch01::valueChanged), CCControlEventValueChanged);
    
    return true;

}

void ControlSwitch01::valueChanged(CCObject* sender, CCControlEvent ev)
{
    CCControlSwitch *switchControl = (CCControlSwitch *)sender;
    if(switchControl->isOn())
    {
        CCLOG("This is On");
    }
    else
    {
        CCLOG("This is Off");
    }
}





posted on 2014-05-01 21:31  三少爷的剑123  阅读(147)  评论(0编辑  收藏  举报

导航