顺序循环通过正方形

时间:2015-09-14 18:28:27

标签: arrays swift sprite-kit

我有一个正方形网格,如下所示,我试图弄清楚如何循环每一行(从下到上或反之亦然),选择一个随机方块并模拟一个按(向下缩小然后向上) 。我已经将每行创建为5个SKShapeNodes的数组,其代码如下:

广场之行:

//Row Seven
        for var i = -2; i < 3; i++ {

            firstSquare = SKShapeNode(rectOfSize: CGSize(width: self.view!.frame.size.width * (1/5), height: self.view!.frame.size.width * (1/5)))
            firstSquare.fillColor = SKColor(hue: 225.0/360.0, saturation: 0.1176, brightness: 0.2667, alpha: 1.0)
            firstSquare.strokeColor = SKColor.clearColor()
            firstSquare.position = CGPoint(
                x: CGRectGetMidX(self.frame) + ((firstSquare.frame.size.width + CGFloat(2)) * CGFloat(i)),
                y: (CGRectGetMidY(self.frame) + self.frame.size.height / 4) - (firstSquare.frame.size.width + CGFloat(2)) * CGFloat(6))
            rowSeven.append(firstSquare)
            addChild(firstSquare)

        }

七行中的每一行都被添加到一个包含所有行的数组(gameLayers)中。

gameLayers = [rowOne, rowTwo, rowThree, rowFour, rowFive, rowSix, rowSeven]

我有以下代码循环遍历gameLayers数组并选择eachRow,选择一个随机元素并运行一个按下的动画。

private func randomIntergerInRange(low:Int, high: Int) -> Int {
        let randomGeneratedNumber = low + Int(arc4random()) % (high - low);
        return Int(randomGeneratedNumber)
    }

func loopThrough() {

for eachRow in gameLayers {

    let selectedSquare = eachRow[randomIntergerInRange(0, high: eachRow.count)]

    let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(2) * Double(NSEC_PER_SEC)))
        dispatch_after(delay, dispatch_get_main_queue()) {

            let scaleDown = SKAction.scaleTo(0.85, duration: 0.3)
            let scaleUp = SKAction.scaleTo(1.0, duration: 0.3)
            let scalingSequence = SKAction.sequence([scaleDown, scaleUp])

            selectedSquare.runAction(scalingSequence)

    }
}

}

尝试此操作时,在每一行中选择一个正方形,但所有选定的正方形同时运行动画,而不是按照我的意愿顺序运行。

任何想法可能是什么原因。我认为for循环会依次为每个元素运行动画。

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为问题可以通过递归循环使用。在一行上的正方形上运行动画后,调用相同的函数并更新行以启动动画。

var selectedRowIndex = 0

    func loopThrough() {
            let rowSelected = gameLayers[selectedRowIndex] //Select a row from the gameLayers


            let selectedNumber = self.randomIntergerInRange(0, high: rowSelected.count)
            let squareSelected = rowSelected[selectedNumber] //Select a square from the selected Row. 

            //Run the animation on that square
            let scaleDown = SKAction.scaleTo(0.9, duration: 0.2)
            let scaleUp = SKAction.scaleTo(1.0, duration: 0.2)
            let scalingSequence = SKAction.sequence([scaleDown,
                    scaleUp])

            squareSelected.runAction(scalingSequence)


            let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(1.3) * Double(NSEC_PER_SEC)))
                dispatch_after(delay, dispatch_get_main_queue()) {

//Delay for 1.3 seconds and call the function again. Increase the index of the selected row to select the next 
                    if self.selectedRowIndex != self.gameLayers.count - 1 {
                        self.selectedRowIndex++
                        self. loopThrough()
                    }
            }
     }