我正在尝试创建一个回合制游戏,直到13轮结束。然而,在我创建了一个无限的while循环之后,它只会在13轮之后中断,所以场景永远不会被加载。为什么didMoveToView
中的代码在场景显示之前执行?有没有办法解决这个问题,或者我可能做错了什么?
override func didMoveToView(view: SKView) {
/* Setup your scene here */
view.ignoresSiblingOrder = true
roundFirstCard = Card(key: "2c")
var actions = [SKAction]()
let cards = makeDeck()
for c in cards {
let card = Card(key: c)
card.name = card.key
card.position = CGPointMake(500, 400)
addChild(card)
giveCardToCorrectPlayer(cards.indexOf(c)!, c: card)
actions.append(SKAction.runAction(SKAction.moveTo(getTargetLocationForCard(cards.indexOf(c)!), duration: 1.0), onChildWithName: card.name!))
actions.append(SKAction.waitForDuration(0.05))
}
actions.append(SKAction.waitForDuration(1.0))
playerCardSets = [player1Cards, player2Cards, player3Cards, player4Cards]
self.runAction(SKAction.sequence(actions), completion: {self.spreadCards()})
currentPlayer = 1
while true {}
}
答案 0 :(得分:0)
你不需要一个while循环,特别是如果你的场景是一个圆形。
所以我想你在你的问题中写的这个场景是你游戏的典型轮次。
您可以在NSUserDefaults
中保存轮次编号,例如在AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "roundCounter")
NSUserDefaults.standardUserDefaults().synchronize()
return true
}
所以在你的场景中,你知道你的回合是什么:
override func didMoveToView(view: SKView) {
/* Setup your scene here */
var roundCounter = NSUserDefaults.standardUserDefaults().integerForKey("roundCounter")
roundCounter += 1 //Start new round
guard let r = roundCounter where r > 13 {
gameOver() //launch gameOver function
return
}
// do your stuff
// save the actual round number
NSUserDefaults.standardUserDefaults().setInteger(roundCounter, forKey: "roundCounter")
NSUserDefaults.standardUserDefaults().synchronize()
}
func gameOver() {
// reset to zero your round counter
NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "roundCounter")
NSUserDefaults.standardUserDefaults().synchronize()
//do your stuff
}