krzysztofzablocki / CCNode-SFGestureRecognizers

Adding UIGestureRecognizers to cocos2d, painless.
http://twitter.com/merowing_
Other
202 stars 34 forks source link

Update for Cocos2d 3.0 #8

Closed PaulSolt closed 10 years ago

PaulSolt commented 10 years ago

I have it working for Cocos2d 3.0 sample project, but I'm not exactly how to add legacy support. The legacy projects do not build with these changes.

How do you recommend handling the API differences from 2.x to 3.x?

  1. CCArray doesn't exist in 3.0 I think this can be safely used as a normal for loop on 1.x and 2.x

    for(object in array)
  2. userInteractionEnabled has been added to CCNode

    I'm not sure the best approach for adding backwards compatibility with the API change from 2.x to 3.x. I get build errors if I try to do this:

    if([self respondsToSelector:@selector(isRunning)]) {
        BOOL value = [self isRunning];
    }
PaulSolt commented 10 years ago

The two new methods are

userInteractionEnabled
and
isRunningInActiveScene
on CCNode.

Should those properties be overridden and then called from the forwardInvocation: and methodSignatureForSelector: methods?

https://github.com/krzysztofzablocki/CCNode-SFGestureRecognizers/blob/master/CCNode%2BSFGestureRecognizers/CCNode%2BSFGestureRecognizers.m#L525

krzysztofzablocki commented 10 years ago

I think you could implement it by just keeping old isTouchEnabled inside all code, just modify code for forwardInvocation and methodSignature, so if selector is not found then you first check if the instance supports the 3.0 methods. If so then you use them. Does that make sense?

Something along the lines of:

if (![self respondsToSelector:aSelector]) {
    if (aSelector == @selector(isTouchEnabled)) {
      if([self respondsToSelector:@selector(userInteractionEnabled)]) {
            return [self methodSignatureForSelector:@selector(userInteractionEnabled)];
      }
      return [self methodSignatureForSelector:@selector(sf_isTouchEnabled)];
...
PaulSolt commented 10 years ago

Ok, it's working for Cocos2d 1.x, 2.x, and 3.x.

I didn't change the forwardInvocation and methodSignature. Instead I added calls to check if the current instance responds to a selector and changed the methods:

 - (BOOL)sf_isPointInArea:(CGPoint)pt

and

 - (BOOL)isPointTouchableInArea:(CGPoint)pt

to call the appropriate method (isRunning, isRunningInActiveScene, and isUserInteractionEnabled).