处理SpriteKit Swift中的每个节点

时间:2015-11-03 15:49:07

标签: iphone swift sprite-kit

每次点击屏幕时,我都会创建一个新的SKShapeNode。

我试着做下一件事:每当新的SKShapeNode落地(地面y是屏幕的一半)时 - 他变成绿色。 所以我尝试了这个:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
newestBall = SKShapeNode(circleOfRadius: 10)
        newestBall.physicsBody = SKPhysicsBody(circleOfRadius: newestBall.frame.height / 2)
        newestBall.position = CGPoint(x: CGRectGetMidX(self.frame) ,y: self.frame.size.height - newBall.frame.height)
        newestBall.fillColor = SKColor.blueColor()
        newestBall.strokeColor = SKColor.blackColor()
        newestBall.glowWidth = 1.0
        newestBall.zPosition = 9
        newestBall.physicsBody?.dynamic = true
        newestBall.physicsBody?.allowsRotation = false
        newestBall.physicsBody?.friction = 0
        newestBall.physicsBody?.restitution = 1
        newestBall.physicsBody?.linearDamping = 0
        newestBall.physicsBody?.angularDamping = 0
        newestBall.physicsBody?.categoryBitMask = ballsGroup
}
override func update(currentTime: CFTimeInterval) {
if newestBall.position.y < ground.position.y {
            newestBall.fillColor = SKColor.greenColor()
        }
}

它有效!但是每当我点击两次并创造2个球(并且第一个球仍然没有在地下),只有最后一个球变为绿色,因为他是newestBall

如果他在地下,我想让每一个球变绿。

1 个答案:

答案 0 :(得分:2)

为您的newestBall命名:

newestBall.name = "Ball"

然后检查名称=&#34; Ball&#34;的所有节点。并改变颜色符合要求

self.enumerateChildNodesWithName("Ball", usingBlock: {
        (node: SKNode!, stop: UnsafeMutablePointer <ObjCBool>) -> Void in
        let shapeNode = node as! SKShapeNode
        if shapeNode < self.ground.position.y {

            shapeNode = SKColor.greenColor()
        }
    })

您可以将其添加到update()selfparentNode或您的最新球和地面节点。