Switf Sprite remove NODE

#Swift Sprite Kit# ## Remove Node, if Node leave View## set ``` var newSprit = SKSpriteNode() ``` create function for Node create ``` func createNewSprite(point : CGPoint) { newSprit = SKSpriteNode(color: UIColor.orange, size: CGSize(width: 50, height: 50)) newSprit.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 50, height: 50)) newSprit.position = point self.addChild(newSprit) } ``` on ```override func update(_ currentTime: TimeInterval)``` add new code ``` self.enumerateChildNodes(withName: String, using: (SKNode, UnsafeMutablePointer) -> Void) ``` check your View first, if your View setup by Portrait, you may check your node leave view by point X if your View setup by Landscape, you may check your node leave view by point y Code: ``` self.enumerateChildNodes(withName: "TEST") { (node : SKNode, nil) in if node.position.x < -320 || node.position.x > 320 { print("remove node") node.removeFromParent() } } ``` on the code we know ``` enumerateChildNodes(withName: "???") ``` so we need give our node a name ###How### back to function createNewSprite, and add new line ``` newSprit.name = "TEST" ``` this sample set the name is TEST, so we can back to ``` enumerateChildNodes ``` and write the name

留言

這個網誌中的熱門文章

Easy Triangle Calculator

Powershell transfert UPS Worldship address book to csv file for UPS web import

Swift Sprite Kit easy to create bullet