Overloading framework methods in objective-c, or retaining local changes with framework updates
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:
- 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.
- 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.
- 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.
I can think of a few ways to do this:
- subclass and override certain methods. This is preferable if it is possible.
slightly hackier methods:
- Class Posing provides a way to switch out classes
- Method Swizzling will let you switch out specific methods
精彩评论