// // ToyTouchableSprite.cpp // SteveMaggieCpp // // Created by Katarzyna Kalinowska-Górska on 17.05.2017. // // #include #include "cocos2d.h" #include "ToyTouchableSprite.h" #include "ToyGeometryUtils.h" ToyTouchableSprite* ToyTouchableSprite::createWithFile(const std::string& filename) { ToyTouchableSprite * sprite = new (std::nothrow) ToyTouchableSprite(); if(sprite && sprite->initWithFile(filename)) { sprite->autorelease(); return sprite; } CC_SAFE_DELETE(sprite); return nullptr; } bool ToyTouchableSprite::initWithFile(const std::string& filename) { if(!cocos2d::Sprite::initWithFile(filename)){ return false; } this->interactionEnabled = true; this->touchBeganCallback = [](ToyTouchableSprite* sprite){}; this->configureTouchHandlers(); return true; } void ToyTouchableSprite::configureTouchHandlers() { auto touchListener = cocos2d::EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event){ if(interactionEnabled && ToyGeometryUtils::getBoundingBoxToWorld(this).containsPoint(touch->getLocation())){ this->touchBeganCallback(this); return true; } return false; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); }