如何更改此功能,因为它每5秒更新一次?

时间:2015-03-16 17:19:21

标签: ios swift sprite-kit

override func update(currentTime: CFTimeInterval) {
        var choice = arc4random_uniform(2)
        if (choice == 0) {

        var redBall = SKSpriteNode(imageNamed: "redBall")
        redBall.setScale(0.5)
        redBall.position = CGPointMake(self.size.width/2, self.size.height)
        redBall.size.height = 20
        redBall.size.width = 30
        redBall.physicsBody = SKPhysicsBody(circleOfRadius: redBall.size.height / 2.75)
        redBall.physicsBody?.dynamic = true
        self.addChild(redBall)

    }

    if (choice == 1) {
        var redBall = SKSpriteNode(imageNamed: "redBall")
        redBall.setScale(0.5)
        redBall.position = CGPointMake(self.size.width/5, self.size.height)
        redBall.size.height = 20
        redBall.size.width = 30
        redBall.physicsBody = SKPhysicsBody(circleOfRadius: redBall.size.height / 2.75)
        redBall.physicsBody?.dynamic = true
        self.addChild(redBall)

    }

    if (choice == 2) {
        var redBall = SKSpriteNode(imageNamed: "redBall")
        redBall.setScale(0.5)
        redBall.position = CGPointMake(self.size.width*5, self.size.height)
        redBall.size.height = 20
        redBall.size.width = 30
        redBall.physicsBody = SKPhysicsBody(circleOfRadius: redBall.size.height / 2.75)
        redBall.physicsBody?.dynamic = true
        self.addChild(redBall)

    }

}

1 个答案:

答案 0 :(得分:1)

您只需使用每五秒触发一次的SKAction,而不是使用update功能。

func yourFunction (){
    //Your code
}

override func didMoveToView(view: SKView) {
    super.didMoveToView(view)

    let updateAction = SKAction.runBlock { () -> Void in
        self.yourFunction()
    }

    let waitDuration : NSTimeInterval = 5.0
    let updateAndWaitAction = SKAction.sequence([updateAction,SKAction.waitForDuration(waitDuration)])
    let repeatForeverAction = SKAction.repeatActionForever(updateAndWaitAction)
    self.runAction(repeatForeverAction)

}