简体   繁体   English

依次更改SKAction.wait(forDuration:)

[英]Change SKAction.wait(forDuration: ) in sequence

I have this. 我有这个。 It runs, but now I have to change the forDuration: 10 in maybe 5 . 它可以运行,但是现在我必须更改forDuration: 10 5 forDuration: 10 In the action sequence are 4 sprites that move their path for a given time. 在动作序列中有4个精灵在给定的时间内移动其路径。 Once I run the sequence I can not change the forDuration: 10 一旦运行序列,就无法更改forDuration: 10

Is the anybody out there, who can help me? 有人可以在那里帮助我吗?

let s1_1 = SKAction.run{let unit = S1(); unit.gegnerSpeed = 100; unit.moveAlongPath(self.pfadGegner);self.addChild(unit)}
let s1_2 = SKAction.run{let unit = S1(); unit.gegnerSpeed = 80; unit.moveAlongPath(self.pfadGegner);self.addChild(unit)}


let action1 = SKAction.sequence([
                s1_1, SKAction.wait(forDuration: 10),
                s1_2, SKAction.wait(forDuration: 10),
                s1_1, SKAction.wait(forDuration: 10),
                s1_2, SKAction.wait(forDuration: 10)    ])
            let sequenz = SKAction.sequence([action1, SKAction.run {
                self.letzterGegnerLos = true
                }])
            run(SKAction.repeat(sequenz, count: 1))

Thanks a lot. 非常感谢。

I am not sure why you are running actions on your scene to run actions, you should have your actions on specific nodes instead. 我不确定为什么要在场景中运行动作来运行动作,而是应该在特定节点上执行动作。 But that said, once you assign a sequence, you can't change the actions inside of it as far as I am aware. 但这就是说,一旦分配了一个序列,就我所知,就无法更改其内部的动作。 This means you have to remove the sequence, and assign a new sequence with a new wait duration. 这意味着您必须删除序列,并为新序列分配新的等待时间。

Or do you? 还是你

In a case like yours, it is possible to manipulate the speed property of either the action, or the node. 在像您这样的情况下,可以操纵动作或节点的speed属性。 To manipulate the speed of the action, you need to extract the action using node.action(forKey:"key") . 要控制动作的速度,您需要使用node.action(forKey:"key")提取动作。 The only problem with this, is that you manipulate the speed on all the actions inside the sequence. 唯一的问题是,您可以控制序列中所有动作的速度。

Instead, let's change the speed of the node. 相反,让我们更改节点的速度。

To do this, I recommend using a variable to assign the speed: 为此,我建议使用变量来分配速度:

let dSpeed = 1
let action1 = SKAction.sequence([

            s1_1,
            SKAction.run{[unowned self] in self.speed = dSpeed}, 
            SKAction.wait(forDuration: 10),
            SKAction.run{[unowned self] in self.speed = 1}, 
            s1_2,
            SKAction.run{[unowned self] in self.speed = dSpeed}, 
            SKAction.wait(forDuration: 10),
            SKAction.run{[unowned self] in self.speed = 1},
            s1_1, 
            SKAction.run{[unowned self] in self.speed = dSpeed},
            SKAction.wait(forDuration: 10),
            SKAction.run{[unowned self] in self.speed = 1},
            s1_2, 
            SKAction.run{[unowned self] in self.speed = dSpeed},
            SKAction.wait(forDuration: 10),
            SKAction.run{[unowned self] in self.speed = 1}    ])
        let sequenz = SKAction.sequence([action1, SKAction.run {
            self.letzterGegnerLos = true
            }])
        run(SKAction.repeat(sequenz, count: 1))

Of course, now we have a problem with the scene running fast. 当然,现在我们在场景快速运行方面遇到了问题。 This is why I am questioning why you are running things on your scene. 这就是为什么我要质疑为什么要在场景中运行事物的原因。

From the looks of your code, you need a spawning node to spawn units. 从代码的外观来看,您需要一个生成节点来生成单元。 Let's create that. 让我们来创建它。

lazy var spawnNode = {
                         [unowned self] in
                         let node = SKNode()
                         self.addChild(node)
                         return node
                     }()

As soon as you access spawnNode , it will create it and add it to the scene for you. 一旦您访问spawnNode ,它将创建它并将其添加到场景中。

Next, we set up your spawning functionality. 接下来,我们设置您的生成功能。

func spawn(_ speed:int)
{
    let unit = S1()
    unit.gegnerSpeed = speed
    unit.moveAlongPath(self.pfadGegner)
    self.addChild(unit)
}

Finally, let's set up your action 最后,让我们开始行动

let s1_1 = SKAction.run{spawn(100)}
let s1_2 = SKAction.run{spawn(80)}
let dSpeed = 1
let action1 = SKAction.sequence([

            s1_1,
            SKAction.run{spawnNode.speed = dSpeed}, 
            SKAction.wait(forDuration: 10),
            SKAction.run{spawnNode.speed = 1}, 
            s1_2,
            SKAction.run{spawnNode.speed = dSpeed}, 
            SKAction.wait(forDuration: 10),
            SKAction.run{spawnNode.speed = 1},
            s1_1, 
            SKAction.run{spawnNode.speed = dSpeed},
            SKAction.wait(forDuration: 10),
            SKAction.run{spawnNode.speed = 1},
            s1_2, 
            SKAction.run{spawnNode.speed = dSpeed},
            SKAction.wait(forDuration: 10),
            SKAction.run{spawnNode.speed = 1}    ])
        let sequenz = SKAction.sequence([action1, SKAction.run {
            self.letzterGegnerLos = true
            }])
        run(SKAction.repeat(sequenz, count: 1))

Now, if we want the node to spawn twice as fast, we do dSpeed = 2 . 现在,如果我们希望节点产生速度快两倍,则执行dSpeed = 2 If we want the node to spawn twice as slow, we do dSpeed = 0.5 . 如果我们希望节点产生速度慢两倍,则执行dSpeed = 0.5 By doing this in this fashion, we are not manipulating the speed of the scene in any way. 通过这种方式,我们不会以任何方式操纵场景的速度。

Just a note, the reason I did the speed changes before and after a run block instead of at the beginning and end of the sequence is in case you needed to do any animations. 请注意,我在运行块之前和之后而不是在序列的开头和结尾进行速度更改的原因是,以防万一您需要进行任何动画处理。 The way the code is set up now, run blocks fire immediately, so you can get away with setting dSpeed in the beginning, and 1 at the end. 现在设置代码的方式立即运行块,因此您可以避免在开头设置dSpeed,在最后设置1。 But if for some reason, you place a move action right after you spawn a node, that move action will use the speed of spawn node. 但是,如果由于某种原因在生成节点之后立即放置了移动动作,则该移动动作将使用生成节点的速度。 So if you are only going to do run blocks, then this is acceptable. 因此,如果您只打算执行运行块,那么这是可以接受的。

let action1 = SKAction.sequence([
        SKAction.run{spawnNode.speed = dSpeed}, 
        s1_1,     
        SKAction.wait(forDuration: 10),
        s1_2,
        SKAction.wait(forDuration: 10),
        s1_1, 
        SKAction.wait(forDuration: 10),
        s1_2, 
        SKAction.wait(forDuration: 10),
        SKAction.run{spawnNode.speed = 1}    ])
    let sequenz = SKAction.sequence([action1, SKAction.run {
        self.letzterGegnerLos = true
        }])
    run(SKAction.repeat(sequenz, count: 1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM