我已将我的 scoreNode 作为我的角色必须避免的随机选择的动画球的子节点,并且 scoreNode 正在闪烁(当我将其设置为可见的蓝色时)并且在角色和 scoreNode 之间计算了太多的接触.所以基本上我认为它只是在每个动画帧生成分数,而不仅仅是一个常量节点。如何将其更改为正常获取分数并且节点停止闪烁并获取太多碰撞的位置?
附言我是 swift 的新手,我尽量让这些问题变得清晰,我可以忍受我 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 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()
和
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)
想不通,求帮助
Best Answer-推荐答案 strong>
正如 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()
}
}
关于ios - 为什么我的节点闪烁? ( swift 得分),我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/39007840/
|