同时运行2个动作?游戏冻结?

时间:2023-01-18 19:56:10

Alright, so I've tried a few different things here and nothing is working. I have a dozen sprite nodes that each need to:

好吧,所以我在这里尝试了一些不同的东西,没有任何工作。我有十几个精灵节点,每个节点需要:

  1. wait for a delay
  2. 等待延迟

  3. repeat this forever:
  4. 永远重复这个:

a. move down the screen WHILE scaling to a bigger size (i.e. these 2 happen simultaneously)

一个。在缩放到更大尺寸的同时向下移动屏幕(即这两个同时发生)

b. THEN move up the screen WHILE scaling back to original size (these happen simultaneously also)

湾然后向上移动屏幕,同时缩小到原始尺寸(这些也同时发生)

Normally I thought this would work with a sequence and grouping the scaling and moving actions in 2 blocks, but it hasn't worked. The following gets the node to do it once:

通常我认为这将适用于序列并将缩放和移动动作分组为2个块,但它没有奏效。以下命令使节点执行一次:

 block1.runAction(SKAction.runBlock({
            self.block1.runAction(moveDownLeft)
            self.block1.runAction(scaleBlock)
            let nodelay = 0 * Double(NSEC_PER_SEC)
            let notime = dispatch_time(DISPATCH_TIME_NOW, Int64(nodelay))
            //during delay
            dispatch_after(notime, dispatch_get_main_queue()) {
                //after delay
                self.block1.runAction(moveUpLeft)
                self.block1.runAction(scaleBlockBack)
            }
        })
        )

but when I introduce the repeatActionForever, the whole game freezes.

但是当我介绍repeatActionForever时,整个游戏都冻结了。

Why am I not able to loop the existing code forever? How can I run the 2 actions simultaneously?

为什么我无法永远循环现有代码?如何同时运行2个动作?

2 个解决方案

#1


The big issue you are having is the block does not wait. It just runs the code. So what is happening is you are starting the actions it hits the end of the block and because you are doing repeat forever it calls it again even though the actions are not finished. This is causing that block to be called as fast as it can.

你遇到的最大问题是阻止不等待。它只是运行代码。所以发生的事情是你正在开始它触及块结束的动作,并且因为你正在重复,它会再次调用它,即使动作没有完成。这导致尽可能快地调用该块。

I am not entirely sure where you want the delay but something like this should work or get you started in the right direction.

我不完全确定你想要延迟的地方,但这样的事情应该起作用或让你开始朝着正确的方向前进。

let delay = SKAction.waitForDuration(4.0) //whatever your delay is

let scaleDownGroup = SKAction.group([moveDownLeft, scaleBlock])
let scaleUpGroup = SKAction.group([moveUpLeft, scaleBlockBack])

let sequence = SKAction.sequence([scaleDownGroup, delay, scaleUpGroup])

let repeat = SKAction.repeatActionForever(sequence)

self.block1.runAction(repeat)

Hopefully that helps.

希望这会有所帮助。

#2


  • SKAction.group(_:) runs several SKActions in parallel.
  • SKAction.group(_ :)并行运行几个SKActions。

  • SKAction.waitForDuration(_:) makes a delay.
  • SKAction.waitForDuration(_ :)会延迟。

Use a combination of these to generate your desired effect. Your dispatch_after call is the problem, although I'm not sure what exactly.

使用这些组合可以产生所需的效果。你的dispatch_after调用是个问题,虽然我不确定到底是什么。

#1


The big issue you are having is the block does not wait. It just runs the code. So what is happening is you are starting the actions it hits the end of the block and because you are doing repeat forever it calls it again even though the actions are not finished. This is causing that block to be called as fast as it can.

你遇到的最大问题是阻止不等待。它只是运行代码。所以发生的事情是你正在开始它触及块结束的动作,并且因为你正在重复,它会再次调用它,即使动作没有完成。这导致尽可能快地调用该块。

I am not entirely sure where you want the delay but something like this should work or get you started in the right direction.

我不完全确定你想要延迟的地方,但这样的事情应该起作用或让你开始朝着正确的方向前进。

let delay = SKAction.waitForDuration(4.0) //whatever your delay is

let scaleDownGroup = SKAction.group([moveDownLeft, scaleBlock])
let scaleUpGroup = SKAction.group([moveUpLeft, scaleBlockBack])

let sequence = SKAction.sequence([scaleDownGroup, delay, scaleUpGroup])

let repeat = SKAction.repeatActionForever(sequence)

self.block1.runAction(repeat)

Hopefully that helps.

希望这会有所帮助。

#2


  • SKAction.group(_:) runs several SKActions in parallel.
  • SKAction.group(_ :)并行运行几个SKActions。

  • SKAction.waitForDuration(_:) makes a delay.
  • SKAction.waitForDuration(_ :)会延迟。

Use a combination of these to generate your desired effect. Your dispatch_after call is the problem, although I'm not sure what exactly.

使用这些组合可以产生所需的效果。你的dispatch_after调用是个问题,虽然我不确定到底是什么。