1

All I need is to know if one finger is touching or is lifted from the trackpad. I guess I have to use NSTouch and NSTouchPhaseBegan and NSTouchPhaseEnded. This is probably a way to do it, but I don't know how to implement this Objective-C code in my Cocos2d-X C++ code. Can anyone please give an example of how to implement this Objective-C code into a Cocos2d-X project as it's own class e.g. trackpad.mm and trackpad.h?

Community
  • 1
  • 1

1 Answers1

0

Cocos2d X have user input EventListner. You can Check if User have touched screen , user is touching screen and Dragging it and user have Ended Touch(Lifted Finger from Screen)

Simple way to do is Use Event Listner.

Define this in .H File

cocos2d::EventListenerTouchOneByOne *_touchListener;
bool onTouchBegan(cocos2d::Touch*, cocos2d::Event*);
void onTouchEnded(cocos2d::Touch*, cocos2d::Event*);
void onTouchMoved(cocos2d::Touch*, cocos2d::Event*);

Now in your .cpp file after Init Method

_touchListener = EventListenerTouchOneByOne::create();
_touchListener->onTouchBegan = CC_CALLBACK_2(GamePlayScene::onTouchBegan, this);
_touchListener->onTouchEnded = CC_CALLBACK_2(GamePlayScene::onTouchEnded, this);
_touchListener->onTouchMoved = CC_CALLBACK_2(GamePlayScene::onTouchMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this);

Define this 3 Methods in Code

// trigger when you push down
bool GamePlayScene::onTouchBegan(Touch* touch, Event* event)
{
return true;
}
// trigger when moving touch
void GamePlayScene::onTouchMoved(Touch* touch, Event* event)
{   
}
// trigger when you let up
void GamePlayScene::onTouchEnded(Touch* touch, Event* event)
{
//Code to Get Touch Location where user have ended touch
Point location = touch->getLocationInView();
location = Director::getInstance()->convertToGL(location);

CCLOG("Position x -> %f , y -> %f",location.x,location.y);
}
  • and you do not want to use NSTouch if you willing to port your app to other platform other then iOS. – Maulik Pankhanya Aug 12 '16 at 15:36
  • NSTouch exists outside iOS. The question is asking about an application running on Mac OS. –  Aug 12 '16 at 15:58
  • Method I suggested you will work on all cocos 2dx Supported Platform and you will not require to work on Bridge Class. – Maulik Pankhanya Aug 14 '16 at 10:01
  • Thanks for the answers! But the Event listener doesn't register if finger is slightly touching the Apple Trackpad, just when it is pressed down as a mouse button press. – Stian Remvik Aug 15 '16 at 08:20
  • I have managed to call objective-c code in my Cocos2d-x c++ app by following [this](http://techbirds.in/how-to-call-objective-c-from-c-in-cocos2d-x/) guide. But I can't find a way to use the NSEvent method and touchesMatchingPhase:NSTouchPhaseBegan like described [here](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html) – Stian Remvik Oct 06 '16 at 12:43