Swift Sprite Kit easy to create bullet
Swift Sprite Kit
Easy create bullet
How to easy create bullet like picture
Create SkSpriteNode
var bullet = SKSpriteNode()
Add SKSpriteNode to View
func bulletFire(){ bullet = SKSpriteNode(color: UIColor.green, size: CGSize(width: 5, height: 50)) bullet.name = "fireBullet" addChild(bullet) bullet.zPosition = 2 let move = SKAction.moveTo(y: self.frame.height, duration: 1) bullet.run(move) }
Set remove Node after Node leave View
self.enumerateChildNodes(withName: "fireBullet") { (node : SKNode, nil) in if node.position.y > self.frame.height / 2 { node.removeFromParent() } }
Add Node to TouchesBegin
override func touchesBegan(_ touches: Set<uitouch>, with event: UIEvent?) { for t in touches { self.touchDown(atPoint: t.location(in: self)) var pos : CGPoint = t.location(in: self)
}bulletFire() }
留言
張貼留言