views:

42

answers:

1

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.

+1  A: 

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:

cobbal
You say "override certain methods". How do you go about that? Won't you get compile errors if you redefine the method?
Jeff B
Ah, I think I just realized my mistake. I have been reading that Objective-C does not allow overloading. I misinterpreted that as redefining a method.
Jeff B
@Jeff B: In fact, you can redefine a method without even subclassing. A method in a category takes precedence over a method defined in a class. (Though if you have the same method defined in *two* categories, no telling which one will be called.)
Chuck
Cool, and thanks!
Jeff B
Oh, sorry, one more question. What about the shadowSprite variable I am adding to the Node? I don't believe you can do that with categories.
Jeff B
you should use a subclass for that case, categories can not (to my knowledge) have instance variables.
cobbal