Change time interval in SKAction.waitForDuration()

2019-03-01 11:47发布

Basically this is one of the actions in a repeating sequence of actions. Each time the action gets called up in the sequence, i want the waiting time to increase, so i added a counter to act as the waiting time number and increment whenever the action gets called up in the sequence. The problem is with this, is that the variable only increments when the sequence starts, but doesn't change at all when the action happens again in the sequence, The waiting time remains constant throughout the game. and i can't figure out how to get it to change when it gets called up

var timeInterval = 0
//main method
override func didMoveToView(view: SKView) {
    var scale = SKAction.scaleTo(5,duration: 2)
    sprite = spriteSet(texture: SKTexture(imageNamed: "texture"), color: UIColor.brownColor(), size: CGSizeMake(100,100))
    sprite.runAction(SKAction.repeatActionForever(SKAction.sequence([waitAction,scale,waitAction])))
}
//I want the time to increase each time this function is called in the sequence
func waitFunction() -> SKAction {
    timeInterval++
    return SKAction.waitForDuration(NSTimeInterval(timeInterval))
}

1条回答
趁早两清
2楼-- · 2019-03-01 12:02

You can use the recursive way to accomplish what you want (increment duration of wait parameter):

class GameScene:SKScene {

    var wait:NSTimeInterval = 0

    override func didMoveToView(view: SKView) {

        NSLog("Start") //Using NSLog just to print the current time
        recursive()

    }

    func recursive(){

        let recursive = SKAction.sequence([

            SKAction.waitForDuration(++wait),
            //Do your stuff here, eg. run scale, move...
            SKAction.runBlock({[unowned self] in NSLog("Block executed"); self.recursive()})
            ])

        runAction(recursive, withKey: "aKey")
    }
}

What you are currently experiencing, is that wait action is created, initialized and reused in an action sequence. You have to rebuild an action each time.

查看更多
登录 后发表回答