zoukankan      html  css  js  c++  java
  • Cocos2d-x3.0游戏实例之《别救我》第六篇——从代码中获取UI控件

     

    这篇的内容非常easy,获取UI控件,然后使用它。

     

    还记得我们在UI编辑器中给三个button分别命名了吧?

    如今要用上了。

     

    笨木头花心贡献,啥?花心?不呢,是用心~

    转载请注明,原文地址: http://www.benmutou.com/blog/archives/918

    文章来源:笨木头与游戏开发

     

    依据名字查找控件

    首先给TollgateScene再include一些头文件,不然等会编译又报错了:

    1. #include "editor-support/cocostudio/CCSGUIReader.h"
    2. #include "cocostudio/CocoStudio.h"
    3. #include "ui/CocosGUI.h"
    4. using namespace cocos2d::ui;
    5. using namespace cocostudio;

     

    上面就是比較完整的使用UI所须要用到的头文件了。

     

    然后,获取UI控件的方法例如以下,继续改动createOprUI函数:

    1. void TollgateScene::createOprUI()
    2. {
    3.     auto UI = cocostudio::GUIReader::getInstance()->widgetFromJsonFile("OprUI_1.ExportJson");
    4.     this->addChild(UI);
    5.  
    6.     /* 获取button对象 */
    7.     Button* rightBtn = (Button*)Helper::seekWidgetByName(UI, "rightBtn");
    8.     Button* quickMoveBtn = (Button*)Helper::seekWidgetByName(UI, "quickMoveBtn");
    9.     Button* leftBtn = (Button*)Helper::seekWidgetByName(UI, "leftBtn");
    10. }

    Helper::seekWidgetByName函数会从UI里面找控件,一层层的找。父控件找不到,就找子控件。如此递归。最后找的名字相符的控件,返回这个控件对象。

     

    非常easy,不多解释喇~

     

    加入button回调事件

    OK,最后一步了,如今button摆在那里什么都做不了,我们给button加入回调事件~

     

    先给TollgateScene加入三个函数声明:

    1.     void moveToLeft(Ref* sender, TouchEventType type);
    2.     void moveToRight(Ref* sender, TouchEventType type);
    3.     void quickMove(Ref* sender, TouchEventType type);

    这是Button点击事件回调时所须要的函数格式。

     

    然后。继续改动createOprUI函数:

    1. void TollgateScene::createOprUI()
    2. {
    3.     auto UI = cocostudio::GUIReader::getInstance()->widgetFromJsonFile("OprUI_1.ExportJson");
    4.     this->addChild(UI);
    5.  
    6.     /* 获取button对象 */
    7.     Button* rightBtn = (Button*)Helper::seekWidgetByName(UI, "rightBtn");
    8.     Button* quickMoveBtn = (Button*)Helper::seekWidgetByName(UI, "quickMoveBtn");
    9.     Button* leftBtn = (Button*)Helper::seekWidgetByName(UI, "leftBtn");
    10.  
    11.     /* 加入button回调事件 */
    12.     leftBtn->addTouchEventListener(this, toucheventselector(TollgateScene::moveToLeft));
    13.     rightBtn->addTouchEventListener(this, toucheventselector(TollgateScene::moveToRight));
    14.     quickMoveBtn->addTouchEventListener(this, toucheventselector(TollgateScene::quickMove));
    15. }

     

    利用addTouchEventListener函数就能够绑定button的回调事件了~

     

    最后了。看看三个回调函数的实现:

    1. void TollgateScene::moveToLeft(Ref* sender, TouchEventType type)
    2. {
    3.     switch (type)
    4.     {
    5.     case TOUCH_EVENT_ENDED:
    6.         m_player->moveToLeft();
    7.         break;
    8.  
    9.     }
    10. }
    11.  
    12. void TollgateScene::moveToRight(Ref* sender, TouchEventType type)
    13. {
    14.     switch (type)
    15.     {
    16.     case TOUCH_EVENT_ENDED:
    17.         m_player->moveToRight();
    18.         break;
    19.  
    20.     }
    21. }
    22.  
    23. void TollgateScene::quickMove(Ref* sender, TouchEventType type)
    24. {
    25.     switch (type)
    26.     {
    27.     case TOUCH_EVENT_ENDED:
    28.         m_player->quickMove();
    29.         break;
    30.  
    31.     }
    32. }

    是不是感觉有点小复杂?

    应该说。有点小麻烦,由于button事件绑定的时候。是没有区分“按下”、“移动”、“松开”的,所以我们要自己推断一下,TOUCH_EVENT_ENDED就是button点击,然后松开的时候的事件。

    假设大家认为麻烦,能够自己改源代码,加入一些函数,在绑定button事件的时候。能够指定绑定哪种事件。

    以及能够使用std::function来作为參数,这样非常方便,当然,跑题了。为了避免大家混乱,这里就不介绍了。

     

    执行測试

    OK,如今大家执行游戏。然后点击这三个操作button,看看主角是不是能左右移动以及放屁(向下冲)吧~

     

     

    下一篇,加入碰撞检測,让主角碰到墙壁之后,进行加血。

    没错,就是加血。不是扣血~由于《别救我》胜利的条件是血量为0。碰到墙是要惩处的~

    惩处的方式就是加血~

     

     

  • 相关阅读:
    Elementary Methods in Number Theory Exercise 1.2.25
    Elementary Methods in Number Theory Exercise 1.2.14
    图解欧几里德算法
    图解欧几里德算法
    Elementary Methods in Number Theory Exercise 1.2.14
    Android中的长度单位详解(dp、sp、px、in、pt、mm)
    分享下多年积累的对JAVA程序员成长之路的总结
    android异常之都是deamon惹的祸The connection to adb is down, and a severe error has occured.
    TomatoCartv1.1.8.2部署时报错
    JavaScript浏览器对象之二Document对象
  • 原文地址:https://www.cnblogs.com/llguanli/p/6762776.html
Copyright © 2011-2022 走看看