1

Hello

Just to give a little context, I am trying to make a Mancala game in ios. I created the UI in the GameScene.sks file.

xCode Screenshot

I have each SKNode pit (pit0 is the bottom left pit and are numbered counter-clockwise from there)numbered with another SKNode inside of it called chips, which contains all the chip SKSpriteNodes for that pit. I am trying to make it so that when one of the pits is pressed all the chips move 4 pits over.

class GameScene: SKScene {

var touchLocation = CGPoint()
var gameBoard = SKNode()
var pits: Array<Pit> = Array(repeating: Pit(), count: 14)

override func sceneDidLoad() {
    setUpGameBoard()
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        touchLocation = touch.location(in: gameBoard)

        let currentPit: Pit = ((gameBoard.atPoint(touchLocation).parent?.parent) ?? pits[0]) as! Pit
        print(currentPit)

        for i in 0..<14 {
            if currentPit == pits[i] {

                let move1 = SKAction.move(to: pits[i+1].position, duration: 0.5)
                let move2 = SKAction.move(to: pits[i+2].position, duration: 0.5)
                let move3 = SKAction.move(to: pits[i+3].position, duration: 0.5)
                let move4 = SKAction.move(to: pits[i+4].position, duration: 0.5)

                let waitAction = SKAction.wait(forDuration: 0.1)

                let moveSequence = SKAction.sequence([move1, waitAction, move2, waitAction, move3, waitAction, move4])

                currentPit.chipsNode.run(moveSequence)
            }
        }

    }
}

func setUpGameBoard() {
    gameBoard = childNode(withName: "gameBoard")!

    for i in 0..<14 {
        pits[i] = (childNode(withName: "gameBoard")?.childNode(withName: "pit\(i)"))! as! Pit
        pits[i].chipsNode = pits[i].childNode(withName: "chips")!
        pits[i].chips = pits[i].childNode(withName: "chips")?.children as! [Chip]

    }
    print(pits)
}

This code moves the chips but only moves them slightly not to the position where they are supposed to go. I am trying to move the chips SKNode containing all of the chips 4 pits counter-clockwise. Please help me if you can and if you are confused by what I am trying to do comment on this.

Screenshot of the result after pressing pit3

  • Can't tell for sure based on your snippet, but I suspect you're mixing up coordinates. chipsNode is a child of pits[i], so its coordinates are relative to that pit's origin. If the implementation is natural, you probably have chipsNode with position (0,0), i.e., it's at the same spot at the pit. When you move chipsNode to pits[i+4].position, you're moving it to the offset pits[i+4].position _relative to pits[i].position_. If you want to move it to pits[i+4]'s position, the natural way is to remove it from pits[i]'s children and add it as a child of pits[i+4]. – bg2b Nov 14 '19 at 20:38
  • @bg2b I took your advice and tried adding it as a child of the pit, and it works. I am using the .removeFromParent() function to remove the chips from the parent and then using the .addChild(to: someParent) function to add it to the other parent. However, now they just teleport to the position instead of animating. Is there a way to animate it. – richardr1126 Nov 14 '19 at 21:25
  • If you have the chips not as a child node of the pit (but instead as children of the board that happen to be at the same position as the pit they're in), then the original move(to:) you wrote is OK. If you want to keep them as children of the pit that they're in, then you'll have to remove them as children of the pit, add them as children of the board at the pit's position, animate the move as you did, and then remove them from the board and add them as children of the target pit (with position reset to 0,0). – bg2b Nov 14 '19 at 22:39

0 Answers0