Swift 3我的游戏代码无法正常工作

时间:2017-02-11 18:05:19

标签: swift function sprite-kit swift3 scene

如果我触摸按钮,游戏就会崩溃。我能做什么?如何?我想忽略除按钮之外的所有触摸。我怎样才能做到这一点? 这是我的接触开始:

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

}

3 个答案:

答案 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")

它仍然看起来很丑,但必须工作

答案 1 :(得分:1)

好吧,坦率地说它有点乱,特别是didBeginContact函数。如果我正确地读你的意思,它应该是这样的:

  • 与&#34;白色&#34;的任何接触阻止应该结束游戏。
  • 对于除一个特定颜色以外的所有其他颜色的联系人应该结束游戏。
  • 联系特定颜色应该会增加分数。

如果这确实是我们的目标,那么下面的内容可能会起作用,尽管我仍然建议进一步重构,以避免在游戏逻辑上建立如此重要的部分来进行flakey字符串匹配:

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()
    }
}

编辑:根据下面的讨论,添加了一些警卫来检查physicsBodies上是否存在节点。

答案 2 :(得分:0)

根据我在其他答案中的讨论,我猜测您的touchesBegan的复杂性会导致您偶尔添加多个版本的播放器精灵。下面应该只添加每个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)
    }
}