Ios 创建自定义类型的SKSpriteNode

Ios 创建自定义类型的SKSpriteNode,ios,cocoa-touch,custom-component,sprite-kit,skspritenode,Ios,Cocoa Touch,Custom Component,Sprite Kit,Skspritenode,我正在制作一个游戏,用户在游戏开始时选择一种武器。 我有一个SK剑类,但我想能够设置它的类型。 我希望实现它,如下所示: JBSword *weapon = [[JBSword alloc]initWithSwordType:HeavySword HandelType:Grip]; 这样我就可以打电话: [weapon specialAttack]; 并且它将执行特定于指定剑的操作(例如HeavySword)选项1 您要做的是将其内置到Objective-C中。在您的Swarm类中定义一个名

我正在制作一个游戏,用户在游戏开始时选择一种武器。 我有一个SK剑类,但我想能够设置它的类型。 我希望实现它,如下所示:

JBSword *weapon = [[JBSword alloc]initWithSwordType:HeavySword HandelType:Grip];
这样我就可以打电话:

[weapon specialAttack];

并且它将执行特定于指定剑的操作(例如HeavySword)

选项1

您要做的是将其内置到Objective-C中。在您的
Swarm
类中定义一个名为
SpecialAtack
的方法:

@interface Sword : SKNode

    -(void) specialAttack;

@end
然后编写一个空白实现:

@implementation Sword

    -(void) specialAttack
    {
        return;
    }

@end
@implementation Sword

-(id) initWithType:(SwordType) type handle:(HandleType) handle
{
    if(self = [super init]) {
        _type = type;     // Use _type within int, and self.type everywhere else
        _handle = handle; // Same thing here.
    }
    return self;
}

-(void) specialAttack
{
    // Code goes here
    // Use self.type and self.handle to access the sword and handle types.
}
现在,使用
swarm
作为基类创建一个名为
HeavySword
的子类:

@interface HeavySword : Sword

    -(void) specialAttack;

@end

...

@implementation HeavySword

-(void) specialAttack
{
    // HeavySword code here
}
现在,只需创建一个类型为
HeavySword
的对象:

Sword *weapon = [[HeavySword alloc] init];

...

[weapon specialAttack];
我还建议使用您自己的前缀,而不是SK,这意味着类是Sprite工具包的一部分

选项2

使用
typedef enum
定义一些常量:

typedef enum {
    kHeavySwordType,
    kLightSwordType,
    kDualSwordType,
    ...
} SwordType;

typedef enum {
    kWoodHandleType,
    kGoldHandleType,
    kDiamondHandleType,
    ...
} HandleType;
然后可以在接口中声明一些属性和init方法:

@interface Sword : SKNode

    @property (nonatomic) SwordType type;
    @property (nonatomic) HandleType handle;

    -(id) initWithType:(SwordType) type handle:(HandleType) handle;

    -(void) specialAttack;

@end
最后,在您的实现中:

@implementation Sword

    -(void) specialAttack
    {
        return;
    }

@end
@implementation Sword

-(id) initWithType:(SwordType) type handle:(HandleType) handle
{
    if(self = [super init]) {
        _type = type;     // Use _type within int, and self.type everywhere else
        _handle = handle; // Same thing here.
    }
    return self;
}

-(void) specialAttack
{
    // Code goes here
    // Use self.type and self.handle to access the sword and handle types.
}

选项1

您要做的是将其内置到Objective-C中。在您的
Swarm
类中定义一个名为
SpecialAtack
的方法:

@interface Sword : SKNode

    -(void) specialAttack;

@end
然后编写一个空白实现:

@implementation Sword

    -(void) specialAttack
    {
        return;
    }

@end
@implementation Sword

-(id) initWithType:(SwordType) type handle:(HandleType) handle
{
    if(self = [super init]) {
        _type = type;     // Use _type within int, and self.type everywhere else
        _handle = handle; // Same thing here.
    }
    return self;
}

-(void) specialAttack
{
    // Code goes here
    // Use self.type and self.handle to access the sword and handle types.
}
现在,使用
swarm
作为基类创建一个名为
HeavySword
的子类:

@interface HeavySword : Sword

    -(void) specialAttack;

@end

...

@implementation HeavySword

-(void) specialAttack
{
    // HeavySword code here
}
现在,只需创建一个类型为
HeavySword
的对象:

Sword *weapon = [[HeavySword alloc] init];

...

[weapon specialAttack];
我还建议使用您自己的前缀,而不是SK,这意味着类是Sprite工具包的一部分

选项2

使用
typedef enum
定义一些常量:

typedef enum {
    kHeavySwordType,
    kLightSwordType,
    kDualSwordType,
    ...
} SwordType;

typedef enum {
    kWoodHandleType,
    kGoldHandleType,
    kDiamondHandleType,
    ...
} HandleType;
然后可以在接口中声明一些属性和init方法:

@interface Sword : SKNode

    @property (nonatomic) SwordType type;
    @property (nonatomic) HandleType handle;

    -(id) initWithType:(SwordType) type handle:(HandleType) handle;

    -(void) specialAttack;

@end
最后,在您的实现中:

@implementation Sword

    -(void) specialAttack
    {
        return;
    }

@end
@implementation Sword

-(id) initWithType:(SwordType) type handle:(HandleType) handle
{
    if(self = [super init]) {
        _type = type;     // Use _type within int, and self.type everywhere else
        _handle = handle; // Same thing here.
    }
    return self;
}

-(void) specialAttack
{
    // Code goes here
    // Use self.type and self.handle to access the sword and handle types.
}

当我调用[Bear SpecialAtack]时,它会同时调用这两个函数吗?除非在HeavySword实现中调用[super SpecialAtack]。我遇到了一个问题。我需要定义另一个属性(handle),为每把剑的每个handle定义一个不同的子类是荒谬的。(50多个文件)。@JordanBrown好的,如果子类化对您不起作用,我添加了另一个选项。当我调用[Bear SpecialAtack]时,它会调用这两个函数吗?除非您在HeavySword实现中调用[super SpecialAtack]。我遇到了一个问题。我需要定义另一个属性(handle),为每把剑的每个handle定义一个不同的子类是荒谬的。(50多个文件)。@JordanBrown好的,如果子类化不适合你,我添加了另一个选项。