Skip to content

Instantly share code, notes, and snippets.

@kkabdol
Last active January 4, 2017 08:01
Show Gist options
  • Select an option

  • Save kkabdol/917e1868ff976bb4cc2ac88e9408530d to your computer and use it in GitHub Desktop.

Select an option

Save kkabdol/917e1868ff976bb4cc2ac88e9408530d to your computer and use it in GitHub Desktop.
cocos2d touch event
#include "cocos2d.h"
void SomeNode::setup() {
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2( SomeNode::onTouchBegan, this );
listener->onTouchEnded = CC_CALLBACK_2( SomeNode::onTouchEnded, this );
dispatcher->addEventListenerWithSceneGraphPriority( listener, this );
}
bool SomeNode::onTouchBegan( Touch* touch, Event *event )
{
auto layer = this->getChildByName( "childName" );
Vec2 touchLocation = touch->getLocation(); // Get the touch position
touchLocation = layer->getParent()->convertToNodeSpace( touchLocation );
Rect bBox = layer->getBoundingBox();
if( bBox.containsPoint( touchLocation ) == true )
{
return true;
}
return false;
}
void SomeNode::onTouchEnded( cocos2d::Touch* touch, cocos2d::Event *event )
{
auto layer = this->getChildByName( "childName" );
Vec2 touchLocation = touch->getLocation(); // Get the touch position
touchLocation = layer->getParent()->convertToNodeSpace( touchLocation );
Rect bBox = layer->getBoundingBox();
if( bBox.containsPoint( touchLocation ) == true )
{
// do something;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment