Why is my node flashing? (Making a score in swift)

别等时光非礼了梦想. 提交于 2019-12-11 03:33:44

问题


I have placed my scoreNode as a child of the randomly chosen animated balls my character has to avoid and the scoreNode is flashing (when I set it to a visible blue colour) and too many contacts are counted between the character and the scoreNode. So basically I think it is only generating the scores at each animation frame rather than just being a constant node. How do I change this to where it picks up the score normally and the node stops flashing and picking up too many collisions?

p.s. I am new to swift and I try to make these questions as clear as I can so bear with me XD

Here's some code:

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

this is inside my func all balls() :

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

and

    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)

Can't figure it out, need help :(


回答1:


As explained in comments by Knight0fDragon you need to remove the node.

You could make a global array to save the nodes to remove:

var nodesToRemove: [SKNode]!

Then in your collision method do:

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

And in your update method you can remove the nodes in array:

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


来源:https://stackoverflow.com/questions/39007840/why-is-my-node-flashing-making-a-score-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!