0
votes

[self classmethod] in super class and then use it child class I'm making some iPhone game based on cocos2d. In many games I've seen before, the scene restart when user's character die or gone. It can be done simply but needs some handling if implement in each child class.

[[CCDirector sharedDirector] replacescene:[childclass scene]];

But if I could implement in parent class, it can be done by once. I guess. So I tried [self scene] in parent class, but it doesn't work.

How can I do this in parent class?

added in

@interface Parent : CCLayer  {}

+(CCScene*)scene;

-(void)dead;

@end

@implementation Parent

...

+(CCScene*)scene {

CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
Parent *layer = [Parent node];

// add layer as a child to scene
[scene addChild: layer];

// return the scene
return scene;

}

...

-(void)dead {

// called after character death

[[CCDirector sharedDirector] replacescene:[self scene]]; //This makes a error "Parent may not respond to 'scene'"

}


@interface Child1 : Parent {}

+(CCScene*)scene;

@end

@implementation Parent

...

+(CCScene*)scene {

CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
Child *layer = [Child node];

// add layer as a child to scene
[scene addChild: layer];

// return the scene
return scene;

}

...

@end

I think Child1 doesn't need dead method implementation because it's child class of Parent but it doesn't work. And Child2, Child3...too.

It works when I move the dead method to Child1 and modify its self -> Child1

Please let me know, if u can't understand my question. thank u for reply.

1
Your question is vague ... What exactly are you trying to achievegiorashc
I suspect you are not using "child class", "parent class", "class method", and such terms correctlyuser102008

1 Answers

1
votes

Have found the solution I wanted.

-(void) dead { // if your character died, call this method

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:2.0 scene:[self.class scene]]];

}

In dead method, self can be child class inherited from super class.

ex) super class is Stage : CCLayer, and child class is Stage1 : Stage dead method is invoked when your character die in Stage1,Stage2,Stage3 ... You don't need to implement each dead method in those class(Stage1,Stage2..) like [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:2.0 scene:[StageX scene]]];