CCLayer with gestures (TAP,DOUBLE TAP, SWIPE supported) IOS (Android should also work)

Hi,

I’ve written a simple gestures extention for cclayer.
At the moment swipe, tap and double tap are working :slight_smile:
For those interested or looking for a class like this, i have attached it to this message.


XTLayer.zip (3.7 KB)

4 Likes

I think it would be nice to have some kind of delegate methods that would allow subclassing of the layer. Some kind of method to be overridden so that it gets called whenever the touch is started, moved, or ended.

Lance Gray wrote:

I think it would be nice to have some kind of delegate methods that would allow subclassing of the layer. Some kind of method to be overridden so that it gets called whenever the touch is started, moved, or ended.

Hello Lance,

The XTLayer subclasses the CCLayer, there are already methods that can be overridden, so i use it like this:

class MyGameClass : public XTLayer

There are some methods that can be overridden:

These are passthroughs of the touchesbegan, moved and ended:
// Callbacks
virtual void xtTouchesBegan(cocos2d::CCSet* _touches, cocos2d::CCEvent* event){}
virtual void xtTouchesMoved(cocos2d::CCSet* _touches, cocos2d::CCEvent* event){}
virtual void xtTouchesEnded(cocos2d::CCSet* _touches, cocos2d::CCEvent* event){}

Easy callbacks if you just want one location:

// Easy touch callbacks
virtual void xtTouchesBegan(CCPoint position){}
virtual void xtTouchesMoved(CCPoint position){}
virtual void xtTouchesEnded(CCPoint position){}

The gesture callbacks:
// Gesture Callbacks
virtual void xtTapGesture(CCPoint position) {}
virtual void xtDoubleTapGesture(CCPoint position) {}
virtual void xtLongTapGesture(CCPoint position) {}
virtual void xtSwipeGesture(XTTouchDirection direction, float distance, float speed) {}

It’s still a work in progress, i’m planning to add pinch to the gestures soon.

Edwin van Poeijer wrote:

Hi,
>
I’ve written a simple gestures extention for cclayer.
At the moment swipe, tap and double tap are working :slight_smile:
For those interested or looking for a class like this, i have attached it to this message.

Thanks for sharing!

This is working great. Thanks!

Hello Edwin, thanks for a great class.:slight_smile:
I wrote about how to use the XTLayer class in Japanese.
http://tks2.net/memo/?p=311

I would like to also use “pinch in” and “pinch out”.
Isn’t the XTLayer class uploaded to GitHub?

Sorry for my bad english.

Tomoaki Shimizu wrote:

Hello Edwin, thanks for a great class.:slight_smile:
I wrote about how to use the XTLayer class in Japanese.
http://tks2.net/memo/?p=311
>
I would like to also use “pinch in” and “pinch out”.
Isn’t the XTLayer class uploaded to GitHub?
>
Sorry for my bad english.

Thanks :slight_smile:
It’s not on github yet but it will be. I first have to finish my new game.
I intent to add more gestures in the future :slight_smile:

Edwin van Poeijer wrote:

Thanks :slight_smile:
It’s not on github yet but it will be. I first have to finish my new game.
I intent to add more gestures in the future :slight_smile:

Thank you!
I’m looking forward to it.:smiley:

Edwin van Poeijer wrote:

Hi,
>
I’ve written a simple gestures extention for cclayer.
At the moment swipe, tap and double tap are working :slight_smile:
For those interested or looking for a class like this, i have attached it to this message.

Hi Edwin XTLayer is not working for me… i am using cocos2d-x 2.1.2

Thanks
yashu

YASHAVANTHA G M wrote:

Edwin van Poeijer wrote:
> Hi,
>
> I’ve written a simple gestures extention for cclayer.
> At the moment swipe, tap and double tap are working :slight_smile:
> For those interested or looking for a class like this, i have attached it to this message.
>
Hi Edwin XTLayer is not working for me… i am using cocos2d-x 2.1.2
>
Thanks
yashu

What is nog working, could you post some code so i can check.

Edwin van Poeijer wrote:

YASHAVANTHA G M wrote:
> Edwin van Poeijer wrote:
> > Hi,
> >
> > I’ve written a simple gestures extention for cclayer.
> > At the moment swipe, tap and double tap are working :slight_smile:
> > For those interested or looking for a class like this, i have attached it to this message.
>
> Hi Edwin XTLayer is not working for me… i am using cocos2d-x 2.1.2
>
> Thanks
> yashu
>
What is nog working, could you post some code so i can check.
Thank you Edwin for your instant reply it works for only when the child is added at index zero…y is that so?
If I had this~~>addChild, it wont work .
But If I add child as this~~>addChild(object,0) it will work…
Can you please explain me why?

Edwin van Poeijer wrote:

YASHAVANTHA G M wrote:
> Edwin van Poeijer wrote:
> > Hi,
> >
> > I’ve written a simple gestures extention for cclayer.
> > At the moment swipe, tap and double tap are working :slight_smile:
> > For those interested or looking for a class like this, i have attached it to this message.
>
> Hi Edwin XTLayer is not working for me… i am using cocos2d-x 2.1.2
>
> Thanks
> yashu
>
What is not working, could you post some code so i can check.

Hi Edwin,
When i add an object of xtlayer as child to my main scene , xtlayer is not getting touches callback, may i know why???

Wow…Great work.Thanks for sharing.

YASHAVANTHA G M wrote:
Edwin,

When i add an object of xtlayer as child to my main scene , xtlayer is not getting touches callback, may i know why???

I’m not sure why it isn’t working, normaly i use this layer as mainlayer and add additional layers to the XTLayer.

Edwin van Poeijer wrote:

YASHAVANTHA G M wrote:
Edwin,
> When i add an object of xtlayer as child to my main scene , xtlayer is not getting touches callback, may i know why???
>
I’m not sure why it isn’t working, normaly i use this layer as mainlayer and add additional layers to the XTLayer.

It is working now, we need to set touch enable to true …

YASHAVANTHA G M wrote:

Edwin van Poeijer wrote:
> YASHAVANTHA G M wrote:
> Edwin,
> > When i add an object of xtlayer as child to my main scene , xtlayer is not getting touches callback, may i know why???
>
> I’m not sure why it isn’t working, normaly i use this layer as mainlayer and add additional layers to the XTLayer.
>
It is working now, we need to set touch enable to true …

Next week i will start on some more features and documentation for this class.I’ll make sure to add this in the docs :slight_smile:

Hi Edwin,
I wanted to thank you for your great job! I think this is a really useful class.
However, I’m having some trouble getting it to work (perhaps I’ve just forgotten something). I changed my main layer from being a CCLayer subclass to XTLayer and I subsequently removed all references to ccTouchBegan, ccTouchEnded and so on. However this leads to a crash when I try to tap, as the application throws the following assertion: “Layer#ccTouchBegan override me”.
After searching for a while, it turns out I still need to implement ccTouchBegan, but if I do so, then the ccTouchBegan method contained in your class is not called anymore.
I’m sure it’s a very stupid question, but how should I proceed?

Thanks!
Sherry Haibara

Sherry Haibara wrote:

Hi Edwin,
I wanted to thank you for your great job! I think this is a really useful class.
However, I’m having some trouble getting it to work (perhaps I’ve just forgotten something). I changed my main layer from being a CCLayer subclass to XTLayer and I subsequently removed all references to ccTouchBegan, ccTouchEnded and so on. However this leads to a crash when I try to tap, as the application throws the following assertion: “Layer#ccTouchBegan override me”.
After searching for a while, it turns out I still need to implement ccTouchBegan, but if I do so, then the ccTouchBegan method contained in your class is not called anymore.
I’m sure it’s a very stupid question, but how should I proceed?
>
Thanks!
Sherry Haibara

Did you override/implement the XTLayer methods for the touches in your class? For example:

// Easy touch callbacks
virtual void xtTouchesBegan(CCPoint position);
virtual void xtTouchesMoved(CCPoint position);
virtual void xtTouchesEnded(CCPoint position);

// Gesture Callbacks
virtual void xtTapGesture(CCPoint position) {}
virtual void xtDoubleTapGesture(CCPoint position) {}
virtual void xtLongTapGesture(CCPoint position) {}
virtual void xtSwipeGesture(XTTouchDirection direction, float distance, float speed) {}

I have used my class in iOS and Android versions of my game Quadro! with good results.
Let me know if you need more help.

Edwin van Poeijer wrote:

Sherry Haibara wrote:
> Hi Edwin,
> I wanted to thank you for your great job! I think this is a really useful class.
> However, I’m having some trouble getting it to work (perhaps I’ve just forgotten something). I changed my main layer from being a CCLayer subclass to XTLayer and I subsequently removed all references to ccTouchBegan, ccTouchEnded and so on. However this leads to a crash when I try to tap, as the application throws the following assertion: “Layer#ccTouchBegan override me”.
> After searching for a while, it turns out I still need to implement ccTouchBegan, but if I do so, then the ccTouchBegan method contained in your class is not called anymore.
> I’m sure it’s a very stupid question, but how should I proceed?
>
> Thanks!
> Sherry Haibara
>
Did you override/implement the XTLayer methods for the touches in your class? For example:
>
// Easy touch callbacks
virtual void xtTouchesBegan(CCPoint position);
virtual void xtTouchesMoved(CCPoint position);
virtual void xtTouchesEnded(CCPoint position);
>
>
// Gesture Callbacks
virtual void xtTapGesture(CCPoint position) {}
virtual void xtDoubleTapGesture(CCPoint position) {}
virtual void xtLongTapGesture(CCPoint position) {}
virtual void xtSwipeGesture(XTTouchDirection direction, float distance, float speed) {}
>
I have used my class in iOS and Android versions of my game Quadro! with good results.
Let me know if you need more help.

Thanks for the quick answer.

I have declared in my class the following two methods:
@ virtual void xtTouchesBegan(CCPoint position);
virtual void xtSwipeGesture(XTTouchDirection dir, float distance, float speed);@

Only the second one has an actual implementation, the first is pretty much empty. In this situation, I’m getting the assertion crash I mentioned above.
If I replace xtTouchesBegan with ccTouchesBegan I don’t have any crash, but clearly my implementation overrides the one you did in your class that is not getting called.

I have tried, It is working. Great work. Thank you.