I have a 2 player space shooter game and am having trouble with the shooting part. I currently have a ship that moves it's x position with your finger. I want it to shoot strait up when the user pushes the "fire" button. So here is my question. How can I instantiate my bullet(SKSpriteNode) at the gun tip and shoot upwards when the user pushes the "fire"?
Asked
Active
Viewed 133 times
-1
-
The seems like it should be pretty straight forward after reading the documentation. What have you tried so far? Can you post some code you've tried? – Scott Thompson May 14 '17 at 05:50
-
It's a bit strange that you have some sort of game running, and yet you are unable to put a sprite on screen and make it move. Did you write the code you have or have you gotten it from elsewhere? Can you post the code you have written so far? – Steve Ives May 15 '17 at 15:34
1 Answers
0
This is pretty basic but you need to:
- Create a new sprite for the bullet
- Position the bullet in the same place as the ship
- Make the bullet move to the top of the screen and then disappear.
e.g.
let bullet = SKSPriteNode(ImageNames: "bullet.png")
bullet.position = ship.position
bullet.zPosition = ship.zPosition - 1 // Bullet is under the ship
let bulletMove = SKAction.moveTo(y: frame.size.height, duration: 2)
let bulletRemove = SKAction.removeFromParent()
addChild(bullet)
bullet.run(SKAction.sequence([bulletMove, bulletRemove])

Steve Ives
- 7,894
- 3
- 24
- 55