将我的敌人类实现到游戏场景

时间:2014-07-21 11:09:30

标签: ios objective-c class sprite-kit

我仍然是编程的新手,我尝试做的是以下内容:所以我将我的敌人从我的游戏场景分离到另一个班级。 在Enemy class.m文件中,我声明了6个方法。每个方法都代表一个新的关卡,可以从游戏场景调用。 所以在方法中我声明了精灵的图像,路径,射击粒子类型等。 以下是EnemyClass.m文件中第1级方法的示例:

+(void)enemiesLevel1
{
EnemyName = @"enemy1";
SKSpriteNode* enemy = [SKSpriteNode spriteNodeWithImageNamed:EnemyName];
pathSpeed = 3;
CGPathRef path = CGPathCreateWithEllipseInRect(CGRectMake(0,0,400,400), NULL);
SKAction *followTrack = [SKAction followPath:path
                                    asOffset:NO
                                orientToPath:YES
                                    duration:pathSpeed];

SKAction *forever = [SKAction repeatActionForever:followTrack];
SKAction *addEnemy = [SKAction runBlock:^{
    [GameScene addChild: enemy];
}];
SKAction *enemySequence = [SKAction sequence:@[addEnemy, forever]];

[GameScene runAction: enemySequence];
}

然而,Xcode表示两个问题:

No known class method for selector "addChild"

No known class method for selector "runAction"

我从GameScene.m调用方法:

[EnemyClass enemiesLevel1]

这可能看起来像一个愚蠢的问题,但我仍然是新的,我将非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

问题是,您应该在GameScene中创建EnemyClasss的实例,然后使用它;你的代码应该是这样的;

在EnemyClass.m中:

-(void)enemiesLevel1
{
EnemyName = @"enemy1";
SKSpriteNode* enemy = [SKSpriteNode spriteNodeWithImageNamed:EnemyName];
pathSpeed = 3;
CGPathRef path = CGPathCreateWithEllipseInRect(CGRectMake(0,0,400,400), NULL);
SKAction *followTrack = [SKAction followPath:path
                                    asOffset:NO
                                orientToPath:YES
                                    duration:pathSpeed];

SKAction *forever = [SKAction repeatActionForever:followTrack];
SKAction *addEnemy = [SKAction runBlock:^{
    [self addChild: enemy];
}];

SKAction *enemySequence = [SKAction sequence:@[addEnemy, forever]];

[self runAction: enemySequence];
}

在GameScene.m中:

EnemyClass *enemy= [[EnemyClass alloc] initWithSize:self.size];

然后你可以使用:

[self addChild: [enemy enemiesLevel1]];