Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Created December 2, 2013 14: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/7749894 to your computer and use it in GitHub Desktop.
Save t-kashima/7749894 to your computer and use it in GitHub Desktop.
#include "CustomButton.h"
CustomButton::CustomButton()
{
}
CustomButton::~CustomButton()
{
}
void CustomButton::enableTouch()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}
void CustomButton::disableTouch()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
CustomButton* CustomButton::create(const char *fileName)
{
CustomButton *button = new CustomButton();
if (button && button->init() && button->initWithFile(fileName)) {
button->autorelease();
button->enableTouch();
return button;
}
CC_SAFE_DELETE(button);
return NULL;
}
CustomButton* createWithSpriteFrameName(const char *frameName)
{
CustomButton *button = new CustomButton();
if (button && button->initWithSpriteFrameName(frameName)) {
button->autorelease();
button->enableTouch();
return button;
}
CC_SAFE_DELETE(button);
return NULL;
}
bool CustomButton::ccTouchBegan(CCTouch *touch, CCEvent *event)
{
CCPoint point = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());
CCPoint convertedPoint = this->getParent()->convertToNodeSpace(point);
// ボタンが押されたか
bool isTouchInSide = this->boundingBox().containsPoint(convertedPoint);
if (isTouchInSide) {
// ボタンが押された時
CCLog("touchDown!!!");
}
return true;
}
void CustomButton::ccTouchMoved(CCTouch *touch, CCEvent *event)
{
}
void CustomButton::ccTouchEnded(CCTouch *touch, CCEvent *event)
{
}
#ifndef __CUSTOM_BUTTON_H__
#define __CUSTOM_BUTTON_H__
#include "cocos2d.h"
using namespace cocos2d;
class CustomButton : public CCSprite, public CCTargetedTouchDelegate
{
public:
CustomButton();
~CustomButton();
static CustomButton* create(const char *fileName);
static CustomButton* createWithSpriteFrameName(const char *frameName);
void enableTouch();
void disableTouch();
private:
virtual bool ccTouchBegan(CCTouch *touch, CCEvent *event);
virtual void ccTouchMoved(CCTouch *touch, CCEvent *event);
virtual void ccTouchEnded(CCTouch *touch, CCEvent *event);
};
#endif // __HELLOWORLD_SCENE_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment