【问题标题】:Select any element in array选择数组中的任何元素
【发布时间】:2015-11-16 00:06:43
【问题描述】:

我正在开发一款游戏,它包含几个在场景中移动的箭头。我设置了一些碰撞,我需要移除与障碍物碰撞的箭头。我设置的内容非常简单且实用,但只有一个箭头,因为它是数组的元素 [0],很明显,如果另一个(不是 [0])触及障碍物,它不会从父元素中移除.尽管需要手动为每个数字分配数组元素,但有什么方法可以将冲突委托给所有元素?

  // 3. react to the contact between the two nodes
        if firstBody.categoryBitMask == barrier?.physicsBody?.categoryBitMask &&
            secondBody.categoryBitMask == arrows[0].physicsBody?.categoryBitMask {
                // Player & barrier

                gameOver(false)
        }  else if firstBody.categoryBitMask == goal?.physicsBody?.categoryBitMask && secondBody.categoryBitMask == arrows[0].physicsBody?.categoryBitMask {
            // Player & Goal

            arrows[0].removeFromParent()
//            gameOver(true)

谢谢!

:)

【问题讨论】:

    标签: arrays swift sprite-kit ios9


    【解决方案1】:

    然后你初始化你的arrowgoal你应该给他们categoryBitMasks

    arrow.physicsBody?.categoryBitMask = 0x1 << 0
    goal.physicsBody?.categoryBitMask = 0x1 << 1
    barrie.physicsBody?.categoryBitMask = 0x1 << 2
    

    我还建议您创建Constants.swift 文件并将您的常量存储在那里。 Constants.swift:

    let kCategoryArrow: UInt32 = 0x1 << 0
    let kCategoryGoal: UInt32 = 0x1 << 1
    let kCategoryBarrie: UInt32 = 0x1 << 2
    

    然后你可以像这样给categoryBitMasks(然后你初始化它们)

    arrow.physicsBody?.categoryBitMask = kCategoryArrow
    goal.physicsBody?.categoryBitMask = kCategoryGoal
    barrier.physicsBody?.categoryBitMask = kCategoryBarrier
    

    最后检查联系方式:

        /* Arrow vs Barrier */
    
        if bodyA.physicsBody?.categoryBitMask == kCategoryArrow && bodyB.physicsBody?.categoryBitMask == kCategoryBarrier {
            gameOver(false)
        } else if bodyA.physicsBody?.categoryBitMask == kCategoryBarrier && bodyB.physicsBody?.categoryBitMask == kCategoryBullet {
            gameOver(false)
        }
    
        /* Arrow vs Goal */
    
        if bodyA.physicsBody?.categoryBitMask == kCategoryArrow && bodyB.physicsBody?.categoryBitMask == kCategoryGoal {
            bodyA.node.removeFromParent()  
        } else if bodyA.physicsBody?.categoryBitMask == kCategoryGoal && bodyB.physicsBody?.categoryBitMask == kCategoryBullet {
            bodyB.node.removeFromParent()
        }
    

    【讨论】:

    • 我已经初始化了所有的categoryBitMask。问题是每个箭头在碰撞时都应该消失,但它们位于数组内。我需要一种解决方案,可以只删除联系人而不是全部。
    • @AlessandroNardinelli 这个解决方案是移除与球门接触的箭头。你甚至不需要一个数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2011-04-04
    • 1970-01-01
    • 2016-09-22
    相关资源
    最近更新 更多