【发布时间】:2011-06-10 04:57:34
【问题描述】:
我正在开发一个 cocos2d 游戏。它有几个从 CCSprite 派生的类 - 敌人、射弹等。对于这些子类中的每一个,它们都需要进一步区分,例如,敌人 1、敌人 2 等。我可以创建一个类,然后创建它的子类,可以通过 [Enemy1 敌人] 之类的东西创建,其中 + (id)enemy 创建了一个 Enemy 对象,然后对其进行自定义并返回它,但我想要做的是告诉 Enemy 类创建一个实例,然后给它我想要的属性(图像,生命值,可见等),然后返回。我想像这样的方法......
+ (id)enemyWithType:(int)aType
{
Enemy *enemy = nil;
switch (aType) {
case 1:
// set up the first enemy type
[enemy initWithFile:@"enemy1.png"];
[enemy setVisible:YES];
[enemy setHitPoints:10];
break;
case 2:
// set up the second type
[enemy initWithFile:@"enemy2.png"];
[enemy setVisible:NO];
[enemy setHitPoints:5];
break;
default:
break;
}
我会通过调用来调用
[Enemy enemyWithType:1];
或者类似的。这是解决这个问题的正确方法吗?我需要它来为所有这些课程工作。在实践中,我的播放器会有一个像 projectileType 这样的属性,所以当我发射弹丸时,我会向 Projectile 类询问该类型之一。
【问题讨论】:
标签: objective-c ios class cocos2d-iphone