【问题标题】:self classmethod in super class and then use it child class超类中的自我类方法,然后使用它的子类
【发布时间】: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


【解决方案1】:

找到了我想要的解决方案。

-(void) dead { // 如果你的角色死了,调用这个方法

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

}

dead方法中,self可以是继承自超类的子类。

( Stage1,Stage2..) like [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:2.0 scene:[StageX scene]]];

【讨论】:

    猜你喜欢
    • 2012-05-24
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 2018-08-22
    相关资源
    最近更新 更多