2016-08-17 47 views
2

我已將我的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) 

不能弄清楚,需要幫助:(

+0

你永遠不會殺死你的節點,所以是的,每一幀觸球隨機物品得分都會上升 – Knight0fDragon

+0

是真的,所以我需要做些什麼來解決它? – Youngiysh27

+0

你需要殺死節點 – Knight0fDragon

回答

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

非常感謝您的先生:) – Youngiysh27