Skip to content

Instantly share code, notes, and snippets.

@belm
Created October 18, 2013 01:53
Show Gist options
  • Save belm/7035308 to your computer and use it in GitHub Desktop.
Save belm/7035308 to your computer and use it in GitHub Desktop.
Mark Sprite touchable
// 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