Swift Sprite Kit easy to create bullet
#Swift Sprite Kit#
##Easy create bullet##
How to easy create bullet like picture
1. Create SkSpriteNode
```
var bullet = SKSpriteNode()
```
2. 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)
}
```
3. 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()
}
}
```
4. Add Node to TouchesBegin
```
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
for t in touches {
self.touchDown(atPoint: t.location(in: self))
var pos : CGPoint = t.location(in: self)
bulletFire()
}
}
```
留言
張貼留言