CCControlSwitch 开关

新建工程,名为testSwitch

修改HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"

#include "cocos-ext.h"

class HelloWorld : public cocos2d::CCLayer

{

public:

    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)

    virtual bool init();


    // there's no 'id' in cpp, so we recommend to return the class instance pointer

    static cocos2d::CCScene* scene();    

  

    // preprocessor macro for "static create()" constructor ( node() deprecated )

    CREATE_FUNC(HelloWorld);

};


#endif // __HELLOWORLD_SCENE_H__

修改HelloWorldScene.cpp

#include "HelloWorldScene.h"

#include "SimpleAudioEngine.h"


using namespace cocos2d;

using namespace CocosDenshion;

using namespace extension;

CCScene* HelloWorld::scene()

{

    // 'scene' is an autorelease object

    CCScene *scene = CCScene::create();

    

    // 'layer' is an autorelease object

    HelloWorld *layer = HelloWorld::create();


    // add layer as a child to scene

    scene->addChild(layer);


    // return the scene

    return scene;

}


// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !CCLayer::init() )

    {

        return false;

    }

    CCControlSwitch *pSwitch=CCControlSwitch::create(CCSprite::create("switch-mask.png"),

                                                     CCSprite::create("switch-on.png"),

                                                     CCSprite::create("switch-off.png"),

                                                     CCSprite::create("switch-thumb.png"),

                                                     CCLabelTTF::create("", "Arial-BoldMT", 16),

                                                     CCLabelTTF::create("", "Arial-BoldMT", 16)

                                                     );

    pSwitch->setPosition(ccp(200,200));

    //设置关闭状态

    pSwitch->setOn(false);

    //设置可操作

    pSwitch->setEnabled(true);

    //获取是否为打开(on)

    CCLOG("是否打开状态:%i",pSwitch->isOn());

    //获取当前开关状态是否为手动拖动开关进行的

    CCLOG("是否手动拖动的开关:%i",pSwitch->hasMoved());

    addChild(pSwitch);


    return true;

}



你可能感兴趣的:(CCControlSwitch 开关)