Playground輸出的代碼 ,注意右側打印出的SpriteNode輸出值,HAPPY_NODE的輸出信息一致.
//MARK:- 擴展SkSpriteNode屬性
extension SKSpriteNode {
func copyWithPhysicsBody()->SKSpriteNode{
let spriteNode = self.copy() as! SKSpriteNode
spriteNode.physicsBody = self.physicsBody
return spriteNode
}
}
那么我們?yōu)楹我M行節(jié)點的復制呢巍耗?WHY? WHY? WHY?
因為我們須用節(jié)點spritenode.copy()把其它Scene(CircleScene.sks)里的節(jié)點拷貝一份叽粹,并把拷貝的這一份節(jié)點加到當前的scene(GameScene.sks)里纵苛,還有一個重要的注意點玄坦,就是如果要在GameScene.sks取得 Overlay內的紅色節(jié)點的準確坐標,須用convert把copy到fgNode的節(jié)點上的紅色節(jié)點坐標轉為GameScene的坐標(<code>let pos = fgNode.convert(sprite.position, from: sprite.parent!)</code>),好像有點兒深奧外恕,其實不復雜的赖草,具體查看以下代碼学少;
//取得CircelScene.sks上的Overlay節(jié)點
var circleAcross: SKSpriteNode!
let overlayScene = SKScene(fileNamed: "CircelScene.sks")!
let overlayNode = overlayScene.childNode(withName: "Overlay")
circleAcross = overlayNode
//復制一份節(jié)點的拷貝,并加在fgNode節(jié)點上
let initialPlatform = circleAcross.copy() as! SKSpriteNode
fgNode.addChild(initialPlatform)
//取得CircelScene.sks上內的Overlay節(jié)點的子節(jié)點名稱為 "red"的所有節(jié)點
animateCoinsInOverlay(overlayNode)
func animateCoinsInOverlay(_ overlay: SKSpriteNode) {
overlay.enumerateChildNodes(withName: "*", using: { (node, stop) in
if node.name == "red" {
node.run(SKAction.repeatForever(self.coinSpecialAnimation))
} else {
node.run(SKAction.repeatForever(self.coinAnimation))
}
})
}
//節(jié)點發(fā)生碰撞時 didBegin(:)添加節(jié)點的特效,須用convert把子節(jié)點坐標coordinate轉到fgNode的坐標上
func emitParticles(name: String, sprite: SKSpriteNode) {
// name 發(fā)生碰撞的特效名稱;
// sprite 發(fā)生碰撞的的節(jié)點 ,如"red"圓形節(jié)點;
// sprite.parent 為overlay
// fgNode.convert 把coin在cicleScene.sks的坐標轉為fgNode上的坐標
let pos = fgNode.convert(sprite.position, from: sprite.parent!)
let particles = SKEmitterNode(fileNamed: name)!
particles.position = pos
particles.zPosition = 3
fgNode.addChild(particles)
}
更多游戲教學:http://www.iFIERO.com