cocos2d-x3.0的touch事件

cocos2d-x3.0 变化不小 弃用了oc的命名规则  还有一种新的写法 lambda (在最后)

//    v2.2            v3.0

    CCDirector      Director

    CCLayer         Layer

    CCScene         Scene

    CCSprite        Sprite



SceneHelloWorld::createScene()

{

    auto scene = Scene::create();

    


    auto layer = HelloWorld::create();


    scene->addChild(layer);

    return scene;

}


bool HelloWorld::init()

{

    if ( !Layer::init() )

    {

        return false;

    }

    //3.0 获得导演的单例  Director::sharedDirector() 改为了Director::getInstance()  但是还是向下兼容的 CC_DEPRECATED_ATTRIBUTE static Director* sharedDirector() { return Director::getInstance(); }

    Size visibleSize = Director::getInstance()->getVisibleSize(); //获得现实区域

    Point origin = Director::getInstance()->getVisibleOrigin();  //获得现实的起点坐标

//   auto cocos2d-x3.0 变动大得一面

//   auto 自动类型变量

//    比如原来要指定变量是int还是float,现在可以用auto,在赋值的时候,编译器自动识别类型。

//    这个是C++11的新标准,你可以参考C++11新标准文档。

    

    //CC_CALLBACK_1 是新的事件回调从 0-3各自有自己的回调参数  细节可以看次篇博客 http://www.2cto.com/kf/201401/275831.html

    auto closeItem = MenuItemImage::create(

                                           "CloseNormal.png",

                                           "CloseSelected.png",

                                           CC_CALLBACK_1(HelloWorld::menuCloseCallbackthis));

    

closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,

                                origin.y + closeItem->getContentSize().height/2));

    auto menu = Menu::create(closeItem, NULL);

    menu->setPosition(Point::ZERO);//以前cppxy 现在左边 都是Point  比如颜色 ccWHITE 也改成了Color3B::WHITE

    this->addChild(menu, 2);

    

    auto label = LabelTTF::create("Hello World""Arial"24);

    

    label->setPosition(Point(origin.x + visibleSize.width/2,

                            origin.y + visibleSize.height - label->getContentSize().height));


    this->addChild(label, 1,101);


    //这种事件的创建方式 比较以前 更加明了 更加直观

//    触摸事件(EventListenerTouch

//    键盘响应事件(EventListenerKeyboard

//    加速记录事件(EventListenerAcceleration

//    鼠标相应事件(EventListenerMouse

 

//    自定义事件(EventListenerCustom

//(移除事件监听器

//移除一个已经被添加了的监听器:

//_eventDispatcher->removeEventListener(listener);

//

//移除当前事件分发器中所有监听器:

//_eventDispatcher->removeAllEventListeners();

    auto listener = EventListenerTouchOneByOne::create();  //创建一个单点触摸事件 :EventListenerTouchAllAtOnce 为多点

    //设置这些事件的的回调方法

    listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBeganthis);

    listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEndedthis);

    listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMovedthis);

    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //事件调度器

  

    return true;

}

void HelloWorld::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event){


    printf("move");

}

bool HelloWorld::onTouchBegan(Touch* touch, Event  *event)

{

    return true;

}

//通过点击结束区域 来设置 lable的位置

void HelloWorld::onTouchEnded(Touch* touch, Event  *event)

{

    auto location = touch->getLocation();

    auto s = getChildByTag(101);

    //一下 只是个旋转角度 移动的 无关紧要

    s->stopAllActions();

    s->runActionMoveTo::create(1Point(location.x, location.y) ) );

    float o = location.x - s->getPosition().x;

    float a = location.y - s->getPosition().y;

    float at = (floatCC_RADIANS_TO_DEGREESatanf( o/a) );

    if( a < 0 )

    {

        if(  o < 0 )

            at = 180 + fabs(at);

        else

            at = 180 - fabs(at);

    }

    s->runActionRotateTo::create(1, at) );

}


void HelloWorld::menuCloseCallback(Ref* pSender)

{

    auto menuItem = static_cast(pSender);  //获得点击的按钮

    printf("pSender-tag==%d",menuItem->getTag());

    MessageBox("你点击了按钮!!","提示");//提示框

}


lambda 写法 挺有意思  下面写的 和上面功能完全一样


bool HelloWorld::init()

{

    if ( !Layer::init() )

    {

        return false;

    }

    Size visibleSize = Director::getInstance()->getVisibleSize(); //获得现实区域

    Point origin = Director::getInstance()->getVisibleOrigin();  //获得现实的起点坐标

    auto closeItem = MenuItemImage::create(

                                           "CloseNormal.png",

                                           "CloseSelected.png",

                                           [](Ref *sender){

                                               auto menuItem = static_cast(sender);  //获得点击的按钮

                                               printf("pSender-tag==%d",menuItem->getTag());

                                               MessageBox("你点击了按钮!!","提示");//提示框

                                           });

                                           

    

closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,

                                origin.y + closeItem->getContentSize().height/2));

    auto menu = Menu::create(closeItem, NULL);

    menu->setPosition(Point::ZERO); 

    this->addChild(menu, 2);

    

    auto label = LabelTTF::create("Hello World""Arial"24);

    

    label->setPosition(Point(origin.x + visibleSize.width/2,

                            origin.y + visibleSize.height - label->getContentSize().height));


    this->addChild(label, 1,101);


    auto listener = EventListenerTouchOneByOne::create();  

    listener->onTouchBegan = [](Touch*touch,Event*event)

    {

    return true

        ;

    };

    listener->onTouchEnded = [](Touch*touch,Event*event){

        auto location = touch->getLocation();

        auto target = static_cast

(event->getCurrentTarget());

        auto s = target->getChildByTag(101);

        

        //一下 只是个旋转角度 移动的 无关紧要

        s->stopAllActions();

        s->runActionMoveTo::create(1Point(location.x, location.y) ) );

        float o = location.x - s->getPosition().x;

        float a = location.y - s->getPosition().y;

        float at = (floatCC_RADIANS_TO_DEGREESatanf( o/a) );

        if( a < 0 )

        {

            if(  o < 0 )

                at = 180 + fabs(at);

            else

                at = 180 - fabs(at);

        }

        s->runActionRotateTo::create(1, at) );

    };

    listener->onTouchMoved = [](Touch*touch,Event*event){

        printf("move");

    };

    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //事件调度器

    return true;

 

}

你可能感兴趣的:(cpp,cocos2d-x)