为什么我的节点闪烁? (快速得分)

时间:2016-08-17 23:38:39

标签: ios swift sprite-kit

我已将我的scoreNode放置为我的角色必须避免的随机选择的动画球的子节点,并且scoreNode闪烁(当我将其设置为可见的蓝色时)并且在角色和scoreNode之间计算太多的联系人。所以基本上我认为它只是在每个动画帧产生分数而不仅仅是一个恒定的节点。如何将其更改为正常获取分数并且节点停止闪烁并拾取太多碰撞的位置?

P.S。我是新手,我试图让这些问题尽可能清楚,因为我可以忍受XD

这里有一些代码:

func didBeginContact(contact: SKPhysicsContact) {
    let firstBody = contact.bodyA
    let secondBody = contact.bodyB

    if firstBody.categoryBitMask == physicsCategory.Score && secondBody.categoryBitMask == physicsCategory.character || firstBody.categoryBitMask == physicsCategory.character && secondBody.categoryBitMask == physicsCategory.Score{

        score += 1
        print(score)
    }
}

这是我的func所有球():

    let scoreNode = SKSpriteNode()

    scoreNode.size = CGSize(width: 100, height: 7000)
    scoreNode.position = CGPoint(x: self.frame.width, y: self.frame.height)
    scoreNode.physicsBody = SKPhysicsBody(rectangleOfSize: scoreNode.size)
    scoreNode.physicsBody?.affectedByGravity = false
    scoreNode.physicsBody?.dynamic = false
    scoreNode.physicsBody?.categoryBitMask = physicsCategory.Score
    scoreNode.physicsBody?.collisionBitMask = 0
    scoreNode.physicsBody?.contactTestBitMask = physicsCategory.character
    scoreNode.color = SKColor.blueColor()

    let ballarray: NSMutableArray = [blkball, brnball, yelball, bluball]
    let randomBall = Int(arc4random_uniform(UInt32(ballarray.count)))

    let randomItem = ballarray[randomBall]

    randomItem.addChild(scoreNode)

    randomItem.runAction(moveandremove)

无法弄明白,需要帮助:(

1 个答案:

答案 0 :(得分:0)

Knight0fDragon的评论中所述,您需要删除该节点。

您可以创建一个全局数组来保存要删除的节点:

var nodesToRemove: [SKNode]!

然后在碰撞方法中执行:

func didBeginContact(contact: SKPhysicsContact) {
    let firstBody = contact.bodyA
    let secondBody = contact.bodyB

    if firstBody.categoryBitMask == physicsCategory.Score && secondBody.categoryBitMask == physicsCategory.character || firstBody.categoryBitMask == physicsCategory.character && secondBody.categoryBitMask == physicsCategory.Score{
        score += 1
        print(score)
        let isNodeA = firstBody.categoryBitMask == physicsCategory.Score
        let myNode = isNodeA ? firstBody.node : secondBody.node
        nodesToRemove.append(myNode)
    }
}

在您的更新方法中,您可以删除数组中的节点:

func update(currentTime: CFTimeInterval)
{
   if nodesToRemove.count > 0 {
      var removed = nodesToRemove.map { $0.removeFromParent() }
      removed.removeAll()
   }
}