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

标签 swift sprite-kit sktexture

假设我有五个这样的骰子:enter image description here

当我点击时,我喜欢用一个点来替换带有两个点的骰子面并且(将 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()

    }


}

最佳答案

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

首先,创建一个名为 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()
    }
}

关于swift - 如何让一个节点按时间顺序替换另一个节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34840077/

相关文章:

ios - WKWebView 和键盘外观 LayoutConstraints 错误的 Swift 问题

swift - 移动时 CAShapeLayer 消失

swift - 有没有一种简单的方法可以将两个不同类型的对象存储在一个容器中?

ios - SKAction 序列是否真的等到 Action 结束?

ios - 这个 SKShapeNode 渐变着色代码有什么问题?

image - 图像文件夹 : create and fill SKShapeNodes, 每个 1 个

swift - Swift 中的 UIAnimation 重复问题

ios - 可以将类型 '__NSArray0' 的值转换为 'CIRectangleFeature'

ios - SpriteKit Pinball Flipper 行为

ios - 带有 SKTexture 的 CIFilter(CIStripesGenerator)?