LoginSignup
4
4

More than 5 years have passed since last update.

CocosStudioのButton_TouchEventの処理 Cocos2d-x_3.4

Posted at

環境

  • Cocos2d-x 3.4
  • CocosStudio for Mac 2.1.2Beta

Sceneの読み込み

以下を参照。

cocoStudioで作成したSceneをLoadする cocos2d-x-3.4 - Qiita

ButtonへのアクセスとTouchEvent処理

1. Buttonへのアクセス

HelloWorld.m
/**
 * ScrollViewの中に配置されているButtonの例
 * CocosStudioで設定しているName: ScrollView_1, Button_1
 */
auto button = mainScene->getChildByName("ScrollView_1")->getChildByName<ui::Button*>("Button_1");

2. TouchEvent

HelloWorld.h
#include "cocos2d.h"
#include "ui/CocosGUI.h"

class HelloWorld : public cocos2d::Layer
{
    // 略
    /**
     * callback method.
     */
    void touchButton(cocos2d::Ref* pSender, cocos2d::ui::Widget::TouchEventType type);
}
HelloWorld.m
#include "ui/CocosGUI.h"
#include "cocostudio/CocoStudio.h"

USING_NS_CC;
using namespace cocostudio;
using namespace ui; // cocos2d::ui

bool HelloWorld::init()
{
    // 略

    /**
     * macro callbackの場合
     */
    button->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchButton, this));

    /**
     * ラムダ式の場合
     */
    button->addTouchEventListener([this](Ref* pSender, cocos2d::ui::Widget::TouchEventType type){
        if (type == cocos2d::ui::Widget::TouchEventType::ENDED)         {
            // 処理
            CCLOG("touch ended.");
        }
    });
    return yes;
}

void HelloWorld::touchButton(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
    // ui::Widget::TouchEventType::BEGAN;
    // ui::Widget::TouchEventType::MOVED;
    // ui::Widget::TouchEventType::CANCELED;
    // ui::Widget::TouchEventType::ENDED;
    if (type == cocos2d::ui::Widget::TouchEventType::ENDED) {
        // 処理
        CCLOG("touch ended.");
    }
}
4
4
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
4
4