[英]Treat every node in SpriteKit Swift
每次單擊屏幕時,我都會新建一個圓形的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
。
如果他掉入地下,我想使每個球變成綠色。
給您的newestBall
一個名字:
newestBall.name = "Ball"
然后檢查所有名稱為“ Ball”的節點,並更改顏色ir的要求
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()
。 self
是parentNode
或您的newBall和Ground節點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.