Overloading framework methods in objective-c, or retaining local changes with framework updates.

Posted by Jeff B on Stack Overflow See other posts from Stack Overflow or by Jeff B
Published on 2010-04-06T17:06:36Z Indexed on 2010/04/06 20:43 UTC
Read the original article Hit count: 277

I am using cocos2d to build an iPhone game. It's mostly done, but along the way I came across some things that I would like to handle better. I am fairly new to Objective C, and honestly I do more Perl scripting in my day-to-day job than anything, so my C skills are a little rusty.

One of them is the fact that I have modified cocos2d files for some specific cases in my game. However, when I update to a new version, I have to manually pull my changes forward.

So, the question is, what is the best way to deal with this?

Some options I have thought of:

  1. Overload/redefine the cocos2d classes. I was under the impression that I cannot overload existing class functions, so I don't think this will work.
  2. Create patches that I re-apply after library updates. I don't think this will work as the new files won't necessarily be the same as the old ones, and if they were, I could just copy the whole file forward.
  3. Turn in my changes to Cocos2d. Not an option as my changes are very specific to my application.

Am I missing something obvious?

UPDATE:

I will explain what I am doing to be more clear.

Cocos2d has a CCNode object, which can contain children, etc. I added a shadow, which is very similar to a child, but handled a little differently. The shadow has an offset from the parent, and translates with it's parent, rotates around it's own center when the parent rotates, etc. The shadow is not included as a true child, however, so given the correct z-index, the shadows can render under ALL other objects, but still move with the parent.

To do this I added addShadow functions to CCNode, and modified the setPosition and setRotate functions to move the shadowSprite:

CCNode.m:

-(id) init 
{ 
        if ((self=[super init]) ) { 
            ...
            shadowSprite_ = nil;
            ...
        }
}

...

-(BOOL) addShadow: (CCNode*) child offset: (CGPoint) offset
{ 
        shadowSprite_ = child;
        shadowSprite_.position = CGPointMake(position_.x+offset.x, position_.y+offset.y);
        return YES;
} 

...

-(void) setRotation: (float)newRotation
{ 
        rotation_ = newRotation; 
        isTransformDirty_ = isInverseDirty_ = YES;

        if(shadowSprite_) { 
                [shadowSprite_ setRotation: newRotation];
        } 
}

There is more, of course, including the prototypes in the .h file, but that is the basics. I don't think I need shadowSprite to be a property, because I don't need to access it after it has been added.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocos2d-iphone