Reputation: 1137
Every time I click the screen I make a new SKShapeNode of a circle.
I tried to make the next thing: Whenever the new SKShapeNode get under the ground (the ground y is half of the screen) - he becomes green. So I tried this:
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()
}
}
And it works! BUT whenever I click twice and create 2 balls (and the first ball still didn't make it under the ground) only the last ball becomes green because he is newestBall
.
I want to make every ball green if he gets under the ground.
Upvotes: 1
Views: 40
Reputation: 974
Give your newestBall
a name:
newestBall.name = "Ball"
Then check throught all nodes with name = "Ball" and change color ir requirements ar met
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()
}
})
You can add that to update()
. self
is parentNode
or your newestBall and ground nodes.
Upvotes: 2