Extension: Gesture Recognizers for cocos2d-x

I get a SIGARBT saying “reference count should be greater than 0” when SwipeGesture is being removed.

I haven’t checked the guts of CCSwipeGestureRecognizer, but probably you are trying to remove the children of that layer between ccTouchBegan and ccTouchEnded invocations. I can’t recall the details, but as far as I remember in that case touch delegate is being deleted twice: the first time you delete it by yourself calling removeAllChildren (recall CCSwipeGestureRecognizer is a child of the layer), the second time it is deleted by Cocos2d itself. The solution is to make all removes after ccTouchEnded is called, probably during the next frame.

**************************_************************__

It seems the current version of CCGestureRecognizer can’t work with gestures attached to not fullscreen nodes, such as random sprites floating somewhere beyond the screen frame. Am I wrong?
Anyway, I’ve added a minor change to fix that (that is convertToNodeSpaceAR):

bool CCGestureRecognizer::isPositionBetweenBounds(CCPoint pos)
{
    return frame.containsPoint(convertToNodeSpaceAR(pos));
}

I can make a pull request in github if this solution is right and acceptable.

Is there a way to reference the target node? Something like

CCPan* panEvent = (CCPan**)gestureEvent;
CCNode** target = panEvent~~>target;
target~~>setPosition(panEvent->location);

Mike M wrote:

Is there a way to reference the target node? Something like
>
CCPan* panEvent = (CCPan**)gestureEvent;
CCNode** target = panEvent~~>target;
>
target~~>setPosition(panEvent->location);

Very good question. Unforunately there is no way to do that for now, but you can create a property inside CCPan class and set it’s value the parent node. In the next version I’m gonna include that and other features too. Thanks.

dear Artavazd Barseghyan,

if i copy paste the same snippet into my HelloWorld(Game Layer is my case)’s init() method…
CCSwipeGestureRecognizer * swipe = CCSwipeGestureRecognizer::create(); swipe->setTarget(this, callfuncO_selector(GameLayer::didSwipe)); swipe->setDirection(kSwipeGestureRecognizerDirectionRight | kSwipeGestureRecognizerDirectionLeft); swipe->setCancelsTouchesInView(true); this->addChild(swipe);

@
void GameLayer::didSwipe(CCObject * obj)
{
}
@

Given that I only need Swipe Gesture so i added CCGestureRecognizer.h/.cpp AND CCSwipeGestureRecognizer.h/.cpp
I get the following errors while compiling
Undefined symbols for architecture i386: "CCGestureRecognizer::setTarget(cocos2d::CCObject*, void (cocos2d::CCObject::*)(cocos2d::CCObject*))", referenced from: GameLayer::init() in GameLayer.o "CCGestureRecognizer::CCGestureRecognizer()", referenced from: CCSwipeGestureRecognizer::create() in GameLayer.o "CCGestureRecognizer::~CCGestureRecognizer()", referenced from: CCSwipeGestureRecognizer::create() in GameLayer.o "vtable for CCSwipeGestureRecognizer", referenced from: CCSwipeGestureRecognizer::create() in GameLayer.o NOTE: a missing vtable usually means the first non-inline virtual member function has no definition. ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)

My HelloWorld(GameLayer) class is inherited from CCLayer
Im working on Cocos2d-x 2.2.0

As I am new in cocos2d-x for android game in window enviroment and i am making a jumping game. I want to jump my player on single tab small jump, on double tap medium jump and triple tap high jump. OR It may be done on Long press duration.

As I divided my touche two parts. Left half for jump and right half for other function. Currently I am only using normal touches event but i want to implement your Extension.

But I am unable to use. I have no Idea How to implement.
Please you can a running example of you extenxion

did anybody finally requested to add this to the main cocos2d-x branch?

Nope. But it will be great if the feature exists in v3.0
This process should be done in new event dispatcher.

Hi…sir…
I am trying to integrate your extension into my project.
But I can know how to use it, since I an newbie on cocos2dx.
Can you show some example used your extension?

Thanks a lot.

I think I am doing everything right but still the swipe gesture is not getting fired… i want the down gesture to work… :expressionless:

pls help

// in init
CCSwipeGestureRecognizer * swipe = CCSwipeGestureRecognizer::create();
swipe->setTarget(this, callfuncO_selector(HelloWorld::didSwipe));
swipe->setDirection(kSwipeGestureRecognizerDirectionDown);
swipe->setCancelsTouchesInView(true);
addChild(swipe);

void HelloWorld::didSwipe(CCObject * obj)
{
CCLOG(“swiped”);

CCSwipe * swipe = (CCSwipe*)obj;
CCPoint p = swipe->location;

CCLOG("x pos: %f, y pos: %f", p.x, p.y);

}

please make 3.0 beta2 ver.

@mruncola Way too late, but maybe someone else will find it useful since I was experiencing the same problem on iOS. iOS doesn’t have multi-touch enabled by default, so you need to do that too: http://www.cocos2d-x.org/wiki/How_to_Enable_Multi-Touch

3.0rc0 not supportted

support for 3.x soon?

When will the gestures be supported in cocos2d-x?

Here is 3.2 version of this repo. yagihiro/cocos2d-x-extensions

Works fine.

@andver
Thank you for sharing.
Can you please show simple example how to set target
I follow the documentation

but I get compilation error

Setting the target

When a gesture is recognized, your specified method gets called. You
can set the method which you want to be called by the following way:

myGesture->setTarget(this, callfuncO_selector(HelloWorld::didRecognizeGesture));

The targeted method must have 1 parameter of CCObject* type and return void:
void didRecognizeGesture(CCObject * obj);
The received parameter type it’s different in every case. For
example, for the swipe gesture it’s a CCSwipe object, for tap gesture
it’s a CCTap, and so on. So don’t forget to cast correctly before
starting to use it:
CCSwipe * swipe = (CCSwipe*)obj;

@sayedtdm
You should use custom selector for your method - callfuncGR_selector. For example:

swipe->setTarget(this, callfuncGR_selector(MyScene::didSwipe));

And method signature like this:

void MyScene::didSwipe(CCGesture* swipe)