Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Created December 20, 2014 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-kashima/0c6c51c90d3df02f2631 to your computer and use it in GitHub Desktop.
Save t-kashima/0c6c51c90d3df02f2631 to your computer and use it in GitHub Desktop.
#include "CustomButton.h"
USING_NS_CC;
CustomButton *CustomButton::createWithFile(const std::string& filename)
{
CustomButton *pRet = new CustomButton();
if (pRet && pRet->initWithFile(filename)) {
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return nullptr;
}
bool CustomButton::initWithFile(const std::string& filename)
{
if (!Sprite::initWithFile(filename)) {
return false;
}
// タッチイベントを登録する
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [this](Touch *touch, Event *event) {
log("onTouchBegan");
// タッチされた場所を取得する
Vec2 touchPos = touch->getLocation();
bool isTouchInSide = this->getBoundingBox().containsPoint(touchPos);
if (isTouchInSide) {
// タッチされた時に縮めて小さくする
this->setScale(0.9f);
this->setColor(Color3B::GRAY);
return true;
}
return false;
};
listener->onTouchMoved = [this](Touch *touch, Event *event) {
log("onTouchMoved");
};
listener->onTouchEnded = [this](Touch *touch, Event *event) {
log("onTouchEnded");
// タッチが離れた時に元に戻す
this->setScale(1.0f);
this->setColor(Color3B::WHITE);
};
getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
#ifndef __CUSTOM_BUTTON_H__
#define __CUSTOM_BUTTON_H__
#include "cocos2d.h"
class CustomButton : public cocos2d::Sprite
{
public:
static CustomButton *createWithFile(const std::string& filename);
protected:
virtual bool initWithFile(const std::string& filename);
};
#endif // __CUSTOM_BUTTON_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment