2017-02-11 51 views
0

如果我觸摸除按鈕之外的遊戲崩潰。我能做什麼?如何?我想忽略除按鈕之外的所有觸摸。我怎樣才能做到這一點? 這裏是我的touchesBegan:斯威夫特3我的代碼遊戲無法正常工作

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let touch = touches.first 
    if let location = touch?.location(in: self), 
     let node = self.nodes(at: location).first { 

     var player = SKSpriteNode() 

     if node.name == "BlueButton" { 
      player = playerB 
      playerB.isHidden = false 

     } else if node.name == "RedButton" { 
      player = playerR 
      playerR.isHidden = false 

     } else if node.name == "YellowButton" { 
      player = playerY 
      playerY.isHidden = false 

     } else if node.name == "GreenButton" { 
      player = playerG 
      playerG.isHidden = false 

     } 
     for sprite in [playerB, playerW, playerR, playerY, playerG] { 
      sprite?.removeFromParent() 
     } 
     player.position = CGPoint(x: 0, y: -60) 
     addChild(player) 
    } 

} 

這裏是我的touchesEnded功能

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    addChild(playerW) 
    playerB.removeFromParent() 
    playerR.removeFromParent() 
    playerY.removeFromParent() 
    playerG.removeFromParent() 

} 
+0

我明白你的英語。我還注意到你的final * else *是* gameOver()*。儘管**沒有**調試你的代碼(道歉)我的問題是這樣的:它總是打這個不管是什麼?更重要的問題是,你能否給我一些我可以驗證和重現的東西?你有沒有做過任何與斷點首次亮相? – dfd

+0

因此,第一次如果我按下右按鈕,它的工作,但比每次打印遊戲結束... –

+0

而且我沒有任何斷點。它只是不正常工作 –

回答

0

從我的其他答案的討論我你的touchesBegan的複雜性導致你偶爾添加一個以上的player-sprite版本。下面應該只在一個時間這應該更容易避免因精靈精靈覆蓋等意想不到的聯繫人添加每個playerSprite 一次只有一個...

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let touch = touches.first 
    if let location = touch?.location(in: self), 
     let node = self.nodes(at: location).first { 

     var player = SKSpriteNode() 

     if node.name == "BlueButton" { 
      player = playerB 
     } else if node.name == "RedButton" { 
      player = playerR 
     } else if node.name == "YellowButton" { 
      player = playerY 
     } else if node.name == "GreenButton" { 
      player = playerG 
     } 
     for sprite in [playerB, playerW, playerR, playerY, playerG] { 
      sprite.removeFromParent() 
     } 
     player.position = CGPoint(x: 0, y: -50) 
     addChild(player) 
    } 
} 
+0

現在它的工作!謝謝!!我真的感激!!!! –

+0

:-)很高興聽到它。 –

+0

仍然不是:/問題是一樣的稍後......如果我按下黃色按鈕後的綠色按鈕,或者在藍色按鈕按下綠色按鈕並反向後:/ –

1

我覺得這個條件

if firstBody.node?.name == "BLUE" && secondBody.node?.name == "BLUEBLOCK" || 
    firstBody.node?.name == "RED" && secondBody.node?.name == "REDBLOCK" || 
    firstBody.node?.name == "YELLOW" && secondBody.node?.name == "YELLOWBLOCK" && 
    firstBody.node?.name == "GREEN" && secondBody.node?.name == "GREENBLOCK" 

應該看起來像

if (firstBody.node?.name == "BLUE" && secondBody.node?.name == "BLUEBLOCK") || 
    (firstBody.node?.name == "RED" && secondBody.node?.name == "REDBLOCK") || 
    (firstBody.node?.name == "YELLOW" && secondBody.node?.name == "YELLOWBLOCK") || 
    (firstBody.node?.name == "GREEN" && secondBody.node?.name == "GREENBLOCK") 

它STIL我看起來很醜,但必須工作

+0

當然,如果所有的節點都有正確的名稱... –

+0

問題是一樣的:/ –

1

好的,坦率地說它有點亂,特別是didBeginContact函數。如果我正確地讀了你的意圖,它應該是這樣的:

  • 任何與「白色」塊的聯繫應該結束遊戲。
  • 對於其他顏色的所有接觸,但一個特定顏色應結束遊戲。
  • 聯繫具體顏色應該增加得分。

如果這確實是我們的目標對像下面可能應該工作,但我還是會建議進一步重構,以避免對古怪的字符串匹配的基礎遊戲邏輯的這樣一個重要組成部分:

func didBegin(_ contact: SKPhysicsContact) { 
    guard let nodeA = contact.bodyA.node else { 
     // this and the one blow are good places to add a breakpoint to inspect what's going on if still having problems 
     return 
    } 
    guard let nodeB = contact.bodyB.node else { 
     return 
    } 

    // Some Bools to make the below logic more readable 
    let blueContacted = nodeA.name == "BLUEBLOCK" || nodeB.name == "BLUEBLOCK" 
    let redContacted = nodeA.name == "REDBLOCK" || nodeB.name == "REDBLOCK" 
    let yellowContacted = nodeA.name == "YELLOWBLOCK" || nodeB.name == "YELLOWBLOCK" 
    let greenContacted = nodeA.name == "GREENBLOCK" || nodeB.name == "GREENBLOCK" 

    var removableNodeName: String? 

    if blueContacted && yellowContacted { 
     removableNodeName = "YELLOWBLOCK" 
    } else if redContacted && greenContacted { 
     removableNodeName = "GREENBLOCK" 
    } else if yellowContacted && blueContacted { 
     removableNodeName = "BLUEBLOCK" 
    } else if greenContacted && redContacted { 
     removableNodeName = "REDBLOCK" 
    } 

    if let nodeName = removableNodeName { 
     score += 1 
     scoreLabel?.text = "\(score)" 
     if nodeA.name == nodeName { 
      nodeA.run(SKAction.move(to: CGPoint(x: 0 , y: 360), duration: 0.3)) 
      nodeA.run(SKAction.fadeOut(withDuration: 0.3)) 
     } else { 
      nodeB.run(SKAction.move(to: CGPoint(x: 0 , y: 360), duration: 0.3)) 
      nodeB.run(SKAction.fadeOut(withDuration: 0.3)) 
     } 
    } else { // Any other contact means game over 
     nodeA.removeFromParent() 
     nodeB.removeFromParent() 
     score = 0 
     scoreLabel?.text = "\(score)" 
     gameOver() 
    } 
} 

編輯:根據下面的討論添加了一些警衛來檢查physicsBody上節點的存在。

+0

我得到一個致命的錯誤消息,當我使用此代碼..(意外地發現零,而解包可選值) –

+0

究竟在哪裏?也許在創造Bools的部分?如果是這樣,這表明你有一些聯繫人註冊你不關心或你沒有設置名稱的節點... –

+0

讓nodeA = contact.bodyA.node!或讓nodeB = contact.bodyB.node!頂部 –