zoukankan      html  css  js  c++  java
  • cocos2d-x之加法计算器

    bool HelloWorld::init()

    {

        if ( !Layer::init() )

        {

            return false;

        }

        

        Size visibleSize = Director::getInstance()->getVisibleSize();

        Vec2 origin = Director::getInstance()->getVisibleOrigin();

        buildUI();

        addListeners();

        

        return true;

    }

    void HelloWorld::buildUI(){

        aTF=TextFieldTTF::textFieldWithPlaceHolder("Value","Courier",16);

        aTF->setPosition(100,300);

        addChild(aTF);

        

        auto addLabel=Label::create();

        addLabel->setString("+");

        addChild(addLabel);

        addLabel->setPosition(aTF->getPositionX()+50,aTF->getPositionY());

        

        bTF=TextFieldTTF::textFieldWithPlaceHolder("Value","Courier",16);

        bTF->setPosition(addLabel->getPositionX()+50,aTF->getPositionY());

        addChild(bTF);

        

        auto equalLabel=Label::create();

        equalLabel->setString("=");

        addChild(equalLabel);

        equalLabel->setPosition(bTF->getPositionX()+50,aTF->getPositionY());

        

        resultLabel=Label::create();

        addChild(resultLabel);

        resultLabel->setPosition(equalLabel->getPositionX()+50,aTF->getPositionY());

        

        addBtn=Label::create();

        addBtn->setString("Add");

        addBtn->setSystemFontSize(16);

        addChild(addBtn);

        addBtn->setPosition(aTF->getPositionX(),aTF->getPositionY()-50);

    }

    void HelloWorld::addListeners(){

        auto director=Director::getInstance();

        auto handler=[=](Touch *t,Event *e){

            auto target=e->getCurrentTarget();

            

            if (target->getBoundingBox().containsPoint(t->getLocation())) {

                if (aTF==target) {

                    aTF->attachWithIME();

                }else if(bTF==target){

                    bTF->attachWithIME();

                }else if (target==addBtn){

                    int a=__String::create(aTF->getString())->intValue();

                    int b=__String::create(bTF->getString())->intValue();

                    

                    resultLabel->setString(StringUtils::format("%d",a+b));

                }

            }

            

            return false;

        };

        

        auto addListenerToTarget=[director,handler](Node *target){

            auto l=EventListenerTouchOneByOne::create();

            l->onTouchBegan=handler;

            director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(l,target);

        };

        addListenerToTarget(addBtn);

        addListenerToTarget(aTF);

        addListenerToTarget(bTF);

        

    //    auto addListener=EventListenerTouchOneByOne::create();

    //    addListener->onTouchBegan=handler;

    //    director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(addListener,addBtn);

    //    

    //    auto aTfClickListener=EventListenerTouchOneByOne::create();

    //    aTfClickListener->onTouchBegan=handler;

    //    director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(aTfClickListener,aTF);

    //    

    //    auto bTfClickListener=EventListenerTouchOneByOne::create();

    //    bTfClickListener->onTouchBegan=handler;

    //    director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(bTfClickListener,bTF);

    }

  • 相关阅读:
    Windows 10 win 10 切换输入法的快捷键
    windows 7输入regedit 打不开注册表
    ios开发之NSData
    ios数组倒序
    iOS NSString使用stringWithFormat的拼接
    BLE 广播格式定义
    低功耗蓝牙UUID三种格式转换
    iOS 如何判断一个点在某个指定区域中
    iOS 毛玻璃效果的实现方法
    iOS毛玻璃效果的实现方法
  • 原文地址:https://www.cnblogs.com/daochong/p/5260187.html
Copyright © 2011-2022 走看看