0

Lets say I have five dice like so:enter image description here

When I tap, Id like the die face with one dot to replace the one with two dots and (moving the 1 die across the row while moving the other die down the row) and if the last die is at the end, get it to replace the first die in the row. Would replacing the SKTextures have something to do with this? Thank you in advance.

Edit:

     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)


    }

}

Edit 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()

    }


}
  • By changing the textures, you lose the ability to uniquely identify each dice (if that's what you want at some point). – Epsilon Jan 17 '16 at 19:30
  • Yes, I would like to identify each one with a physics body (bit masks). Is there another way to do this? – IHeartAppsLLC Jan 17 '16 at 19:31

2 Answers2

1

I think this should work (not tested) assuming your DieFaces are from 1 to 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()
}

Just call this fucntion inside the touchesBegan() function.

Eric
  • 1,210
  • 8
  • 25
1

This approach changes the position of each dice instead of changing its texture. By changing the positions, it maintains the ability to uniquely identify each dice (e.g., by name).

First, create an SKNode called dice and define the size and spacing between each dice.

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

Second, create your dice nodes and add them, in order, to the dice SKNode and set the positions of each dice with

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)

Note that the position of each dice is based on the position of the node within the array.

Lastly, add the following to your code.

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()
    }
}
Epsilon
  • 1,016
  • 1
  • 6
  • 15
  • Okay, well I see a `removeFromParent` in here and thats not what I'm looking to do. Maybe I'm reading it wrong, but I'm just looking to shift the positions, not add/subtract any node from the scene. – IHeartAppsLLC Jan 18 '16 at 00:24
  • It immediately adds the node back into the node tree at the new position (i.e., in front of its siblings). Have you tried the above code? – Epsilon Jan 18 '16 at 01:31
  • Oh okay, now I understand. I haven't yet, sorry I've been busy but I will in just a little bit. – IHeartAppsLLC Jan 18 '16 at 01:33
  • This is giving me an NSInvalidArgumentException: Attempted to add a node which already has a parent. I only have each node called once though. Have you tested this? – IHeartAppsLLC Jan 18 '16 at 05:10
  • The code works. The error occurs when you called `addChild` twice on the same node. I provided the for-loop as an example of how to set the initial positions of the dice. You will need to integrate that into your code. – Epsilon Jan 18 '16 at 08:12
  • Okay, but where does that for loop go? In my didMoveToView? – IHeartAppsLLC Jan 18 '16 at 08:13
  • And I never called 'addChild' twice on the same node. – IHeartAppsLLC Jan 18 '16 at 08:15
  • WAIT ok I figured it out. But its just giving me all of the die faces with one dot on them in a row. How do I fix this? – IHeartAppsLLC Jan 18 '16 at 08:23
  • Is it working now? If not, please post the code that you use to create the dice. – Epsilon Jan 18 '16 at 18:50
  • Yes, it's working perfectly! It just took some tinkering (: thanks so much! – IHeartAppsLLC Jan 18 '16 at 18:51
  • I added `categoryBitMask` to the answer. – Epsilon Jan 19 '16 at 18:27