How to instantiate SKSpriteNode spritekit swift 3

瘦欲@ 提交于 2019-12-12 06:59:33

问题


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"?


回答1:


This is pretty basic but you need to:

  1. Create a new sprite for the bullet
  2. Position the bullet in the same place as the ship
  3. 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])


来源:https://stackoverflow.com/questions/43960262/how-to-instantiate-skspritenode-spritekit-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!