简体   繁体   English

Swift3 SpriteKit:以角度射击子弹

[英]Swift3 SpriteKit: Shooting bullets at angles

Currently my app has an enemy that shoots a bullet which travels from the enemy at whatever x and y position it may be towards the bottom of the screen - the size of the bullet. 目前我的应用程序有一个敌人射击子弹,它可以从敌人的任何x和y位置向屏幕底部移动 - 子弹的大小。 This is an SKAction that happens only once in the enemy trajectory from top to bottom and this trajectories are all at random X positions. 这是一个SKAction,它只在敌人的轨迹中从上到下发生一次,这个轨迹都是随机的X位置。

What I want to do is to shoot 4 bullets, at 4 different angles. 我想做的是以4个不同的角度射出4发子弹。 45, 135, 225 and 270 degrees. 45,135,225和270度。

Currently my fireBullet function is as follows: 目前我的fireBullet功能如下:

func fireBullet() {
    let bullet = EnemyBullet(imageName: bulletImage, bulletSound: bulletSound)
    bullet.name = "Bullet"
    bullet.setScale(1)
    bullet.position = CGPoint(x: enemy.position.x, y: enemy.position.y - bullet.size.height)
    bullet.physicsBody = SKPhysicsBody.init(rectangleOf: bullet.size)
    bullet.physicsBody!.affectedByGravity = false
    bullet.physicsBody!.categoryBitMask = PhysicsCategories.enemy
    bullet.physicsBody!.collisionBitMask = PhysicsCategories.none
    bullet.physicsBody!.contactTestBitMask = PhysicsCategories.player
    gameScene.addChild(bullet)

    let moveBullet = SKAction.moveTo(y: -gameScene.size.height * 0.1, duration: bulletTimeInScreen)
    let deleteBullet = SKAction.removeFromParent()

    let bulletSequence = SKAction.sequence([moveBullet, deleteBullet])
    bullet.run(bulletSequence)
}

I'm assuming that I can change bullet for bullet1 and repeat for bullet2, 3 and 4. With that I can probably use zRotation to rotate each bullet to that specific angle. 我假设我可以为bullet1更改子弹并重复bullet2,3和4.我可以使用zRotation将每个子弹旋转到该特定角度。

What I can not figure out is in which direction to move the bullet. 我无法弄清楚的是移动子弹的方向。 Specifically, how to figure out the X and Y of the SKAction.moveTo from the enemy's position at the time. 具体来说,如何从当时敌人的位置找出SKAction.moveTo的X和Y.

I have searched and read many articles but none that can point me in the right direction. 我搜索并阅读了很多文章,但没有一篇可以指出我正确的方向。

I found a way to do it thanks to this post Shoot bullet in the Direction the Ship is Facing Swift and Sprite Kit by KnightOfDragon which by the way, pointed me in the right direction with trigonometry. 我找到了一种方法可以做到这一点,这要归功于这个帖子拍摄的子弹在方向船上面向Swift和Sprite Kit由KnightOfDragon顺便说一下,用三角学指向我正确的方向。

Basically I changed the variable name from bullet to bullet1 and added bullet2, 3 and 4 changing the zRotation within the same function. 基本上我将变量名称从bullet更改为bullet1,并添加了bullet2,3和4,在同一函数中更改了zRotation。 The moveBullet1 SKAction fires the bullet in the direction the zRotation points at. moveBullet1 SKAction在zRotation指向的方向发射子弹。

The degrees use the extension shown below. 学位使用下面显示的扩展名。 I can't remember where in this site I found that one. 我不记得在这个网站的哪个地方找到了那个。

func fireBullet() {
    let bullet1 = EnemyBullet(imageName: bulletImage, bulletSound: bulletSound)
    bullet1.name = "Bullet"
    bullet1.setScale(scale)
    bullet1.position = CGPoint(x: enemy.position.x, y: enemy.position.y)
    bullet1.zRotation = CGFloat(45.degreesToRadians)
    bullet1.physicsBody = SKPhysicsBody.init(rectangleOf: bullet1.size)
    bullet1.physicsBody!.affectedByGravity = false
    bullet1.physicsBody!.categoryBitMask = PhysicsCategories.enemy
    bullet1.physicsBody!.collisionBitMask = PhysicsCategories.none
    bullet1.physicsBody!.contactTestBitMask = PhysicsCategories.player
    gameScene.addChild(bullet1)

    let moveBullet1 = SKAction.move(to: CGPoint(
        x: distance * cos(bullet1.zRotation) + bullet1.position.x,
        y: distance * sin(bullet1.zRotation) + bullet1.position.y),
        duration: bulletTimeInScreen)
    let deleteBullet1 = SKAction.removeFromParent()

    let bullet1Sequence = SKAction.sequence([moveBullet1, deleteBullet1])

    bullet1.run(bullet1Sequence)
}


extension Int {
    var degreesToRadians: Double { return Double(self) * .pi / 180 }
    var radiansToDegrees: Double { return Double(self) * 180 / .pi }
}

四颗子弹射向不同的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM