2017-02-11 56 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!顶部 –