LoginSignup
13
14

More than 5 years have passed since last update.

Mac版 Cocos Studio 2で作成したGUI(.csb)ファイルをcocos2d-xへ読込み、ButtonとSliderにイベントを追加し利用する。

Last updated at Posted at 2014-11-30

Headerファイル

Scene.cpp
#include "cocostudio/CocoStudio.h"
#include "cocosGUI.h"

*cocostudioって名前間違えやすいから注意

.csbファイルの読込

Scene.cpp

//3.3以降は、createNode   
auto node = CSLoader::getInstance()->createNodeFromProtocolBuffers("Scene.csb");

node->setName("NODE");
this->addChild(node);

*Scene.csbをCocos Studio 2でつくっておく。後述するButtonとSliderも追加しておく。名前はそれぞれ"BTN"と"SLD"とした。
*nodoの名前を"NODE"と付けておいて、別の場所で参照できるようにしてある。

Buttonにイベントを追加

Scene.cpp

auto* btn = dynamic_cast<Button*>(this->getChildByName("NODE")->getChildByName("BTN"));
btn->addTouchEventListener([this](Ref* pSender, ui::Widget::TouchEventType type){

    //touch
    if (type == ui::Widget::TouchEventType::ENDED) {
        //好きな処理
    }
});

*Cocos Studio 2の方でつくったボタンの名前を"Button"と付けてある
*イベントリスナーは、ラムダ式で書いている。thisをキャプチャしておかないと、クラス内の別の関数が呼べない。

Sliderにイベント追加

Scene.cpp
Slider* slider = dynamic_cast<Slider*>(this->getChildByName("NODE")->getChildByName("SLD"));
slider->addEventListener([this](Ref *pSender, Slider::EventType type){

    if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
    {
        Slider* slider = dynamic_cast<Slider*>(pSender);
        int percent = slider->getPercent();
        CCLOG("percent:%d",percent);
    }
});

*Sliderのイベントは、ボタンのTouchEventと違うので注意する。

13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14