0

假设我有五个像这样的骰子:在此处输入图像描述

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

    }


}
4

2 回答 2

1

假设您的 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()函数即可。

于 2016-01-17T17:13:41.640 回答
1

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

首先,创建一个SKNode被调用dice并定义每个骰子之间的大小和间距。

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()
    }
}
于 2016-01-17T20:31:07.393 回答