我有一个执行函数的递归函数,并且在最后一个周期它检查条件,如果条件为真,它将调用另一个也运行操作的类似函数。但是,当第二个func通过递归执行动作时,第一个func开始执行。因此,现在有两个动作同时运行。我阅读了有关GCD的文章并尝试了它,但没有获得预期的结果。(实际上,我仍然不知道如何正确使用它)
第一个主要的func movePiece()
这是我的代码,都在同一个类中:
func movePiece() {
// some Func Calls here.
if ( self.movesRemaining > 0) {
DispatchQueue.global(qos: .userInteractive).async {
self.moveFinished = false
let currentSpace:Int = self.returnPlayerSpace(player: self.whosTurn)
let spaceNumber:Int = currentSpace
var nextSpace:Int = spaceNumber + 1
let moveAction:SKAction = SKAction.move(to: (self.childNode(withName: String(nextSpace))?.position)!, duration: 0.4)
moveAction.timingMode = .easeOut
let wait:SKAction = SKAction.wait(forDuration: 0.2)
let runAction:SKAction = SKAction.run({
if (self.movesRemaining == 1) {
self.KillHim(player:self.whosTurn, nextspace:nextSpace)
DispatchQueue.main.async {
self.moveBackPiece()
}
}
self.setThePlayerSpace(space: nextSpace, player:self.whosTurn)
if (self.movesRemaining == 1) {
self.Adjust(node: self.childNode(withName: String(nextSpace))!)
}
self.movesRemaining = self.movesRemaining - 1
self.movePiece()
})
if self.whosTurn == .Player1 {
self.touchedNode.run(SKAction.sequence([moveAction, wait, runAction]))
} else {
self.playerPiece.run(SKAction.sequence( [moveAction, wait, runAction]))
}
}
}
}
这是第二个函数moveBackPiece():
func moveBackPiece() {
if ((killedPiece == self.Player1Piece1 || killedPiece == self.Player1Piece2 || killedPiece == self.Player1Piece3 || killedPiece == self.Player1Piece4) && self.movesRemainingBack != 1) || ((killedPiece == self.Player2Piece1 || killedPiece == self.Player2Piece2 || killedPiece == self.Player2Piece3 || killedPiece == self.Player2Piece4) && self.movesRemainingBack != 5) || ((killedPiece == self.Player3Piece1 || killedPiece == self.Player3Piece2 || killedPiece == self.Player3Piece3 || killedPiece == self.Player3Piece4) && self.movesRemainingBack != 9) || ((killedPiece == self.Player4Piece1 || killedPiece == self.Player4Piece2 || killedPiece == self.Player4Piece3 || killedPiece == self.Player4Piece4) && self.movesRemainingBack != 13) {
self.movesRemainingBack -= 1
let moveAction:SKAction = SKAction.move(to: (self.childNode(withName: String(self.movesRemainingBack))?.position)!, duration: 0.4)
moveAction.timingMode = .easeOut
let runAction:SKAction = SKAction.run({
if ((self.killedPiece == self.Player1Piece1 || self.killedPiece == self.Player1Piece2 || self.killedPiece == self.Player1Piece3 || self.killedPiece == self.Player1Piece4) && self.movesRemainingBack == 1) || ((self.killedPiece == self.Player2Piece1 || self.killedPiece == self.Player2Piece2 || self.killedPiece == self.Player2Piece3 || self.killedPiece == self.Player2Piece4) && self.movesRemainingBack == 5) || ((self.killedPiece == self.Player3Piece1 || self.killedPiece == self.Player3Piece2 || self.killedPiece == self.Player3Piece3 || self.killedPiece == self.Player3Piece4) && self.movesRemainingBack == 9) || ((self.killedPiece == self.Player4Piece1 || self.killedPiece == self.Player4Piece2 || self.killedPiece == self.Player4Piece3 || self.killedPiece == self.Player4Piece4) && self.movesRemainingBack == 13) {
self.setThePlayerSpaceBack(space: self.movesRemainingBack, Killed: self.killedPiece)
}
self.movesRemaining = self.movesRemaining - 1
self.moveBackPiece()
})
self.killedPiece.run(SKAction.sequence([moveAction, runAction]))
}
}
我该如何使用GCD或其他解决方案? 谢谢您的时间。
答案 0 :(得分:0)
您必须使用GCD组队列。它还将与异步操作一起使用。就像进入->离开,进入->离开,最后它会通知您,这是示例
let queue = DispatchQueue(label: "com.your.text", attributes: .concurrent)
let group = DispatchGroup()
group.enter()
queue.async {
print("1st Operation")
Thread.sleep(forTimeInterval: 10)
print("1st Operatio finished")
group.leave()
}
group.enter()
queue.async {
print("2nd Operation")
Thread.sleep(forTimeInterval: 10)
print("2nd Operation finished")
group.leave()
}
group.notify(queue: .main) {
print("Finished")
}
它很快就写在这里,所以可能会出现语法错误。我希望它能帮助您解决问题。