Using UIGestureRecognizers in cocos2d

cocos2d is one of the best gaming framework I’ve seen, but it doesn’t use UIKit.

When Apple introduced UIGestureRecognizers they unified how touch interaction should work.

Is there a way we can add UIGestureRecognizers to cocos2d architecture, without modyfing cocos2d source?

Some time ago I’ve seen  xemus shared his implementation which is pretty cool, but for me it had a a few things that I do not like:

  • It requires manual modifying cocos2d, and I prefer not to change libraries I’m using so that I can upgrade anytime to newer versions.
  • It uses some wrapper class that I had to be aware of when using gestures, instead of using standard UIGestureRecognizers.

Since I use gesture recognizers in all my cocos2d apps, I’ve rolled my own implementation based on xemus code but I used more dynamic approach.

This is how you add gesture recognizers support for any cocos2d project:

  1. Download my category CCNode+SFGestureRecognizers
  2. import “CCNode+SFGestureRecognizers.h” preferably in your project **Prefix.pch **
  3. Make sure you are using** llvm compiler**. By now you should only be using LLVM.
  4. Now any node can use gesture recognizers, use standard UIGestureRecognizers, there is no need for special treatment when working with cocos2d.

What you get extra:

  1. Each UIGestureRecognizer that is assigned to node have now **node **property so that you can easily use it in your handling code.
  2. Each node has now isTouchEnabled (doesn’t****break CCLayer which already had this property), touchRect properties that you can use to define touchable region in your local space.
  3. If a scene stops running gestures will stop firing as it should ![:)][3] .
  4. The code works with both 2.0 and 1.1 cocos2d versions.

Category and Sample Project is available on GitHub.

You've successfully subscribed to Krzysztof Zabłocki
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.