Spritekit如何在一定时间内运行一个方法,然后启动另一个方法

时间:2014-07-26 20:32:59

标签: objective-c sprite-kit

我正在制作一款游戏,我现在很难知道如何让方法运行一段时间,关闭,然后让另一种方法开始运行。目前我有:

[self spawn];
当场景设置时

这是生成方法:

-(void)spawn {
int xMin = 0;
int xMax = 460;
CGPoint startPoint = CGPointMake(xMin + arc4random_uniform(xMax - xMin),320);
[self performSelector:@selector(spawn) withObject:nil afterDelay:.20];
SKSpriteNode *blue = [SKSpriteNode spriteNodeWithImageNamed:@"whitecircle"];
blue.position = CGPointMake(startPoint.x,startPoint.y);
blue.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:21.5];
blue.physicsBody.usesPreciseCollisionDetection = NO;
blue.physicsBody.categoryBitMask = gainCategory;
blue.physicsBody.contactTestBitMask = playerCategory;
blue.physicsBody.dynamic = NO;
[self addChild:blue];
SKAction *runBlue = [SKAction moveToY:0 duration:1.5];
SKAction *remove = [SKAction removeFromParent];
[blue runAction:[SKAction sequence:@[runBlue,remove]]];
}

我想知道的是如何让这个方法运行一段时间,然后我怎样才能在这个方法结束后启动另一个方法。感谢

1 个答案:

答案 0 :(得分:1)

我认为这就是你要做的事情:

// set duration to seconds you want to wait between spawns
float duration = 1; 

SKAction *actionWait = [SKAction waitForDuration:duration];
SKAction *actionSpawn = [SKAction runBlock:^(void)
{
     [self spawn];
}];

SKAction *actionSequence = [SKAction sequence:@[actionWait, actionSpawn]];
SKAction *actionRepeat = [SKAction repeatActionForever:actionSequence];

[self runAction:actionRepeat];

值得为SKAction提供至少一次良好阅读的班级参考,因此你知道它的能力。

SKAction Class Reference