'Trying to add touch listener to a clipped image
Hi I have tried to cut a square/rectangle image to a circular one using the ClippingNode in cocos2dx but when I try to click on the clipped image and try to get the touch points on it... I didn't get any touched events. In fact I get the same behaviour which is same as clicking outside of the clipped image. Refer the code below. If some one can help me it will be highly appreciated. To check this issue you can add your avatar image and try to clip it and then check the onTouchBegan() function whether it prints Inside clipped Image when touched inside the clipped image.
//Code Implemented
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "ui/CocosGUI.h"
#include<iostream>
using namespace std;
USING_NS_CC;
Scene* HelloWorld::createScene()
{
return HelloWorld::create();
}
bool HelloWorld::init()
{
if ( !Scene::init() )
{
return false;
}
m_clipper = ClippingNode::create();
m_avatarSprite = Sprite::create("guest_avatar_1.png");
//m_avatarSprite->setPosition(Vec2(this->getContentSize().width/2, this->getContentSize().height/2));
//this->addChild(m_avatarSprite);
//CLIPPING NODE
m_clipper->setPosition(Vec2(this->getContentSize().width/3, this->getContentSize().height/3));
m_clipper->addChild(m_avatarSprite);
m_clipper->setTag(10);
this->addChild(m_clipper);
DrawNode *stencil = DrawNode::create();
stencil->drawSolidCircle(Vec2(m_clipper->getContentSize().width / 2,m_clipper->getContentSize().height / 2), m_avatarSprite->getContentSize().height/2, 0, 200, Color4F::BLUE);
m_clipper->setStencil(stencil);
m_clipper->setInverted(false);
m_clipper->retain();
EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded,this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, (Node*)m_clipper);
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
return true;
}
bool HelloWorld::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
Node *clipper = (Node*) this->getChildByTag(10);
std::cout<< "Inside on Touch Began Function" <<endl;
if(clipper->getBoundingBox().containsPoint(touch->getLocation()))
{
std::cout<<"Inside clipped image "<<std::endl;
}
std::cout<<"outside clipped image "<<std::endl;
return true;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|