Last active
January 4, 2017 08:01
-
-
Save kkabdol/917e1868ff976bb4cc2ac88e9408530d to your computer and use it in GitHub Desktop.
cocos2d touch event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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