Sprite kit 如何使自定义操作在特定的时间间隔内执行

Sprite kit 如何使自定义操作在特定的时间间隔内执行,sprite-kit,swift2,skaction,Sprite Kit,Swift2,Skaction,因此,每当我运行此操作时,它都会快速打印hello world 3秒钟(这是我想要的),但打印速度太快。我搞不懂的是如何在动作持续时间内的特定间隔(例如1秒间隔)打印“hello world”。我试着使用睡眠功能,但不起作用 let waitAndPrint = SKAction.customActionWithDuration(3) { _, _ in print("hello world") } 我还没有测试过,但也许像这样的东西会管用 for (

因此,每当我运行此操作时,它都会快速打印hello world 3秒钟(这是我想要的),但打印速度太快。我搞不懂的是如何在动作持续时间内的特定间隔(例如1秒间隔)打印“hello world”。我试着使用睡眠功能,但不起作用

let waitAndPrint = SKAction.customActionWithDuration(3) {
            _, _ in
    print("hello world")    
 }

我还没有测试过,但也许像这样的东西会管用

for (int i = 1; i <= 3; i++) //<-where 3 is the number of seconds over which you want this event to occur. 
{
    [self performSelector:@selector(printHelloWorld:) withObject:self afterDelay:i];
}

-(void) printHelloWorld:(id)sender 
{
    NSLog(@"hello world")
}

for(int i=1;i您可以通过行动实现您想要实现的目标:

let block = SKAction.runBlock{
    println("hello world")
}

scene.runAction(SKAction.repeatAction(SKAction.sequence([block, SKAction.waitForDuration(1.0)]), count: 3))

我知道你说它未经测试,但你有一个错误。如果你使用afterDelay:1,代码将在一秒钟后打印消息三次。你可能打算使用afterDelay:I。此外,这种方法的另一个问题是,即使视图或场景暂停,代码也将继续执行。它不只是循环通过命令在1之后打印吗第二次三次?所以每秒钟三次一次?不。你自己试试。因为你调用performSelector inside for循环,它几乎是瞬时执行的(并且每个时间延迟设置为1秒)。如果你使用afterDelay:i,并且如果你将@selector(printHelloWorld)更改为@selector(printHelloWorld:),它会起作用因为printHelloWord方法中有一个参数。