ardeay
February 17, 2014, 9:49pm
#1
This is what I have been doing in a Class that extends Layer to capture touches (One by one). I have no clue if this is correct, I’ve pieced this together from IRC, google searches, and some blog posts.
In my header Private:
virtual bool onTouchBegan(Touch *pTouches, Event *pEvent);
virtual void onTouchMoved(Touch *pTouches, Event *pEvent);
virtual void onTouchCancelled(Touch *pTouch, Event *pEvent);
virtual void onTouchEnded(Touch *pTouches, Event *pEvent);
//...... .cpp init
this->setTouchEnabled(true);
this->setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
touchListener->onTouchCancelled = touchListener->onTouchEnded;
auto touchListener = EventListenerTouchOneByOne::create();
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);
//.........In my .cpp methods
bool HelloWorld::onTouchBegan( Touch *pTouches, Event *pEvent )
{
Point point = pTouches->getLocationInView();
point = convertCoordinate(point);
if(isPointInCircle(point,kCenter,JOYSTICK_RADIUS)){
isPressed = true;
this->updateVelocity(point);
}
return true;
}
void HelloWorld::onTouchMoved( Touch *pTouches, Event *pEvent )
{
if(isPressed){
//Touch *touch = (Touch*)pTouches->anyObject();
Point point = pTouches->getLocationInView();
//Point point = touch->getLocationInView(touch->view());
point = convertCoordinate(point);
this->updateVelocity(point);
}
}
void HelloWorld::onTouchCancelled( Touch *pTouch, Event *pEvent )
{
this->handleLastTouch();
}
void HelloWorld::onTouchEnded( Touch *pTouches, Event *pEvent )
{
this->handleLastTouch();
}
Thanks!
chezhe
February 18, 2014, 2:26am
#2
there is a variable in Node,
_eventDispatcher,
touch event is charged by it, not director, the code is following as:
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(Miner::touchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(Miner::touchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(Miner::touchEnded, this);
listener->setSwallowTouches(false);
_eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
modify:
//… .cpp init
//this->setTouchEnabled(true); --V3 DEPRECATED
//this->setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->setSwallowTouches(true);
touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
touchListener->onTouchCancelled = touchListener->onTouchEnded;
_eventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);
//…In my .cpp methods
bool HelloWorld::onTouchBegan( Touch *pTouches, Event *pEvent )
{
//Point point = pTouches->getLocationInView();
//point = convertCoordinate(point);
Point point = pTouches->getLocationInView();
if(isPointInCircle(point,kCenter,JOYSTICK_RADIUS)){
isPressed = true;
this->updateVelocity(point);
}
return true;
}
void HelloWorld::onTouchMoved( Touch *pTouches, Event *pEvent )
{
if(isPressed){
//Touch touch = (Touch )pTouches->anyObject();
Point point = pTouches->getLocation();
//Point point = touch->getLocationInView(touch->view());
//point = convertCoordinate(point);
this->updateVelocity(point);
}
}
void HelloWorld::onTouchCancelled( Touch *pTouch, Event *pEvent )
{
this->handleLastTouch();
}
void HelloWorld::onTouchEnded( Touch *pTouches, Event *pEvent )
{
this->handleLastTouch();
}
ardeay
February 18, 2014, 9:12am
#4
@tavandung12 thank you, I updated compiled and ran successfully!