【发布时间】:2012-06-26 05:17:09
【问题描述】:
[self classmethod] 在超类中,然后在子类中使用它
我正在制作一些基于 cocos2d 的 iPhone 游戏。
在我之前见过的许多游戏中,当用户的角色死亡或消失时,场景会重新开始。
它可以简单地完成,但如果在每个子类中实现,则需要一些处理。
[[CCDirector sharedDirector] replacescene:[childclass scene]];
但是如果我可以在父类中实现,它可以一次完成。我猜。
所以我在父类中尝试了[self scene],但它不起作用。
如何在父类中做到这一点?
加入
@interface Parent : CCLayer {}
+(CCScene*)场景;
-(无效)死了;
@结束
@implementation 父级
...
+(CCScene*)场景{
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 {
// 角色死亡后调用
[[CCDirector sharedDirector] replacescene:[self scene]]; //这会报错“Parent may not respond to 'scene'”
}
@interface Child1 : 父级 {}
+(CCScene*)场景;
@结束
@implementation 父级
...
+(CCScene*)场景{
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;
}
...
@结束
我认为 Child1 不需要死方法实现,因为它是 Parent 的子类 但它不起作用。还有 Child2、Child3……也是。
当我将死方法移动到 Child1 并修改其自身时,它可以工作 -> Child1
如果你不能理解我的问题,请告诉我。 谢谢你的回复。
【问题讨论】:
-
你的问题很模糊......你到底想达到什么目的
-
我怀疑你没有正确使用“子类”、“父类”、“类方法”等术语
标签: cocos2d-iphone self scene