随着游戏的进行,更改 SKAction.waitForDuration() 中的时间间隔 [英] Change time interval in SKAction.waitForDuration() as game goes on

查看:7
本文介绍了随着游戏的进行,更改 SKAction.waitForDuration() 中的时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,这是重复动作序列中的动作之一.每次在序列中调用动作时,我希望增加等待时间,因此我添加了一个计数器作为等待时间编号,并在序列中调用动作时递增.问题在于,变量仅在序列开始时增加,但在序列中再次发生动作时根本不会改变,等待时间在整个游戏中保持不变.我不知道当它被调用时如何让它改变

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))
}

推荐答案

你可以使用递归的方式来完成你想要的(增加等待参数的持续时间):

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")
    }
}

您目前正在经历的是,wait 动作是在动作序列中创建、初始化和重用的.你必须每次都重建一个动作.

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.

这篇关于随着游戏的进行,更改 SKAction.waitForDuration() 中的时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆