Created
October 18, 2013 01:53
-
-
Save belm/7035308 to your computer and use it in GitHub Desktop.
Mark Sprite touchable
This file contains 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
// Make hero touchable | |
auto listener1 = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); | |
listener1->setSwallowTouches(true); | |
listener1->onTouchBegan = [](Touch* touch, Event* event){ | |
auto target = static_cast<Sprite*>(event->getCurrentTarget()); | |
Point locationInNode = target->convertToNodeSpace(touch->getLocation()); | |
Size s = target->getContentSize(); | |
Rect rect = Rect(0, 0, s.width, s.height); | |
if (rect.containsPoint(locationInNode)) | |
{ | |
log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y); | |
target->setOpacity(180); | |
return true; | |
} | |
return false; | |
}; | |
listener1->onTouchMoved = [](Touch* touch, Event* event){ | |
auto target = static_cast<Sprite*>(event->getCurrentTarget()); | |
target->setPosition(target->getPosition() + touch->getDelta()); | |
}; | |
listener1->onTouchEnded = [=](Touch* touch, Event* event){ | |
auto target = static_cast<Sprite*>(event->getCurrentTarget()); | |
log("sprite onTouchesEnded.. "); | |
target->setOpacity(255); | |
}; | |
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener1, hero); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment