如何让一个节点按时间顺序替换另一个节点?

How to make a node replace another in chronological order?

假设我有五个这样的骰子:

当我轻敲时,我喜欢用一个点来代替那个有两个点的骰子面并且(将 1 个骰子移过该行,同时将另一个骰子移到该行下方)并且如果最后一个骰子位于最后,将其替换为该行中的第一个骰子。更换 SKTextures 是否与此有关?先感谢您。

编辑:

     override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch: AnyObject in touches {

        let touchLocation = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(touchLocation)
       let texRight = SKAction.setTexture(SKTexture(imageNamed: "DieFace1"))
        DieFace2.runAction(texRight)


    }

}

编辑 2:

    import SpriteKit

     var arrayOfDieFaces = [onE, twO, threE, fouR, fivE]


    class GameScene: SKScene {



      }



    func replace() {
    var count = 1
    while count <= 5 {
        let changeTexture = SKAction.setTexture(SKTexture(imageNamed: "Dice\(count)"))
        if count == 5 {
            arrayOfDieFaces[0].runAction(changeTexture)
        }
        else{
            arrayOfDieFaces[count].runAction(changeTexture)
        }
        count += 1
    }
    arrayOfDieFaces.append(arrayOfDieFaces[0])
    arrayOfDieFaces.dropFirst()

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {





    for touch: AnyObject in touches {

        let touchLocation = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(touchLocation)

        replace()

    }


}

假设您的 DieFaces 从 1 到 6,我认为这应该有效(未测试):

var arrayOfDieFaces = [DieFace1, DieFace2, DieFace3, DieFace4, DieFace5, DieFace6]


func changeTextures() {
    var count = 1
    while count <= 6 {
        let changeTexture = SKAction.setTexture(SKTexture(imageNamed: "DieFace\(count)"))
        if count == 6 {
            arrayOfDieFaces[0].runAction(changeTexture)
        }
        else{
            arrayOfDieFaces[count].runAction(changeTexture)
        }
        count += 1
    }
    arrayOfDieFaces.append(arrayOfDieFaces[0])
    arrayOfDieFaces.dropFirst()
}

只需在 touchesBegan() 函数中调用此函数即可。

这种方法改变了每个骰子的位置,而不是改变它的纹理。通过改变位置,它保持了唯一标识每个骰子的能力(例如,按名称)。

首先,创建一个名为 diceSKNode 并定义每个骰子的大小和间距。

let dice = SKNode()
let diceSize = CGSizeMake(10, 10)
let spacing:CGFloat = 2

其次,创建您的骰子节点并将它们按顺序添加到 dice SKNode 并使用

设置每个骰子的位置
for i in 0..<6 {
    let sprite = SKSpriteNode ...
    sprite.physicsBody = SKPhysicsBody( ...
    sprite.physicsBody?.categoryBitMask = UInt32(0x1 << i)
    sprite.position = CGPointMake(CGFloat(i)*(diceSize.width+spacing) , 0)
    dice.addChild (sprite)
}
// Center SKNode in the view
dice.position = CGPointMake (CGRectGetMidX(view.frame),CGRectGetMidY(view.frame))
addChild (dice)

注意每个骰子的位置是基于数组中节点的位置。

最后,将以下内容添加到您的代码中。

func rotate() {
    // Get last element in the array
    if let last = dice.children.last {
        // Remove the last dice from the SKNode
        last.removeFromParent()
        // Move the last to the front of the array
        dice.insertChild(last, atIndex: 0)
        // Update the positions of the 
        for i in 0..<dice.children.count {
            dice.children[i].position = CGPointMake(CGFloat(i)*(diceSize.width+spacing) , 0)
        }
    }
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {        
    for _ in touches {
        rotate()
    }
}