SKAction在didMovetoview中起作用,但在Function中不起作用

时间:2018-09-25 13:38:43

标签: ios swift function sprite-kit skaction

我之前在here问过这个问题,但由于我对这个问题不太了解,所以无法很好地解释。但是现在在探索并尝试找到解决方案之后,我对它有了更多的了解。

我的主要问题是我需要检查两个球员是否在同一位置,然后再将第一个球员移至本垒打位置,这之前工作正常,但在4个球员和他们的4个棋子可玩后,它现在停止工作。但是,如果我将相同的代码粘贴到didmovetoview中,则可以正常工作。所以代码上没有问题,但是还有其他一些我不了解的基本问题,因为这是我的第一个快速项目。

这是过程:

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

rollDice(player: whosTurn)      

movePiece()
}

用户触摸骰子,掷骰子用户可以触摸玩家棋子后,掷骰子便会通过调用movePiece Func移动。在检查了3个if条件和一个for循环之后,如果2个玩家位于同一位置,它将调用主要功能killhim将该玩家移至其原位,但它不起作用。

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..)

当它仅转1圈时,在添加其他转1圈后仍能正常工作,因为它有时从那里调用rollDice函数,所以它不起作用。

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)

任何帮助都会非常有帮助,谢谢。

0 个答案:

没有答案
相关问题