SKAction Works in didMovetoview but doesn't wo

2019-02-21 02:20发布

问题:

I asked this question previously at here but couldn't explain well because I haven't understood this problem very well. But now after exploring and trying to find the solution I understood it more.

My main problem is I have a func to check if two players at same position if so then move first player to it's home location which is working fine before but after making it playable by 4 players and their 4 pieces it stopped working now. But if I paste the same code in didmovetoview then it works fine. so no problem in code but some other fundamental problem which I don't understand because this is my first swift project.

Here is the process :

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

rollDice(player: whosTurn)      

movePiece()
}

user touches dice, dice rolls after dice roll user can touch player piece and it will move by calling movePiece Func. Where after checking 3 if conditions and one for loop, if 2 players at same position it will call the main func killhim to move that player to it's home position but it isn't working.

func movePiece() {

    if (movesRemaining > 0) {

let currentSpace:Int = returnPlayerSpace(player: whosTurn)

        let spaceNumber:Int = currentSpace
        var nextSpace:Int = spaceNumber + 1

        for node in children {

            if (node.name == String(nextSpace)) {

            let moveAction:SKAction = SKAction.move(to: node.position, duration: 0.5)
            moveAction.timingMode = .easeOut
            let wait:SKAction = SKAction.wait(forDuration: 0.1)
            let runAction:SKAction = SKAction.run({

                if (self.movesRemaining == 1) {

                    self.KillHim(self.whosTurn, nextspace: nextSpace) // The Problem Call

                }

                self.movesRemaining = self.movesRemaining - 1
                self.setThePlayerSpace(space: nextSpace, player:self.whosTurn)

                self.movePiece()

                })
                if whosTurn == .Player1 {

                    touchedNode.run(SKAction.sequence([moveAction, wait, runAction]))

                } else {

                    playerPiece.run(SKAction.sequence([moveAction, wait, runAction]))

                }
            moveFinished = true
                dice.isHidden = true
            }
        }

    } else {

if (self.whosTurn == .Player1) {

            Dice.isHidden = false
            Dice2.isHidden = true

            if (player1Moves == 0) {

                self.whosTurn = .Player2

                rollDice(player: whosTurn)

            }  else {.    // After adding this it all started   

            player1Moves -= 1

          }

        } else if ...............(Continue..)

when it was only for 1 turn it was working fine after adding else for 1 more turn it isn't working because it some times calls rollDice func from there.

func KillHim(_ player:Player, nextspace:Int) {

     if (nextspace == 1 || nextspace == 5 || nextspace == 9 || nextspace == 13 || nextspace == 25) {
       return
    }

if (player == .Player1) {

        if (nextspace == currentSpacePlayer2Piece1) {

            for node in children {

                if node.name == ("5") {

                 let moveAction:SKAction = SKAction.move(to: node.position, duration: 0.5)

                    Player2Piece1.run(moveAction) {
                    print("It Happened")
                }
                    self.currentSpacePlayer2Piece1 = 5   
                    player1Moves += 1
                }
            }

        } else if (nextspace == currentSpacePlayer2Piece2) { ..........(Continue)

Any Help will be really helpful, Thanks.