【问题标题】:End sprite kit collision immediately after first contact第一次接触后立即结束精灵套件碰撞
【发布时间】:2015-02-11 17:11:46
【问题描述】:

您好,我正在尝试改进游戏中两个节点之间碰撞的逻辑。当玩家接触硬币时,我想更新分数并隐藏节点。它工作正常,但分数在与隐藏节点的接触过程中多次更新。我想知道是否有办法只运行一次,以便分数更新一次。这是我的代码

    //MARK: SKPhysicsContactDelegate methods

func didBeginContact(contact: SKPhysicsContact) {

    if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == objectCategory)  {

        gameOver = 1
        movingObjects.speed = 0
        presentGameOverView()
    }

    if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == coinCategory) {
        coinSound()
        contact.bodyB.node?.hidden = true
        score = score + 1
        println(score)
    }
}

【问题讨论】:

  • 你想隐藏节点吗?还是从父母那里移除?

标签: iphone swift ios8 sprite-kit


【解决方案1】:

你可以在增加分数之前检查节点是否隐藏。

func didBeginContact(contact: SKPhysicsContact) {

    if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == objectCategory)  {

        gameOver = 1
        movingObjects.speed = 0
        presentGameOverView()
    }

    if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == coinCategory) {
        if !contact.bodyB.node?.hidden // Added line
        {
           coinSound()
           contact.bodyB.node?.hidden = true
           score = score + 1
           println(score)
        }
    }
}

从父级移除它

func didBeginContact(contact: SKPhysicsContact) {

    if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == objectCategory)  {

        gameOver = 1
        movingObjects.speed = 0
        presentGameOverView()
    }

    if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == coinCategory) {
         if contact.bodyB.node?.parent != nil {
             coinSound()
             contact.bodyB.node?.removeFromParent()
             score = score + 1
             println(score)
         }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多