在船的方向射击子弹Swift和Sprite Kit

时间:2016-03-03 23:00:54

标签: ios swift sprite-kit swift2

我正在制作一个太空射击游戏,我希望这艘船能够朝着它面向的方向发射子弹。我已经弄明白了如何让子弹朝着船的方向旋转,但是我还没有朝着正确的方向射击。此刻,我只拍了拍。我有一个屏幕截图下面。我怎样才能解决这个问题?这是我的代码:

if shootButton.containsPoint(location) {

            let Bullet = SKSpriteNode(imageNamed: "BulletGalaga.png")
            Bullet.zPosition = -5

            Bullet.position = CGPointMake(ship.position.x, ship.position.y)
            Bullet.zRotation = ship.zRotation
            Bullet.zPosition = ship.zPosition

            let action = SKAction.moveToY(self.size.height + 30, duration: 0.8)
            //let action = SKAction.moveTo(self.ship.size.height, duration: 0.8)
            let actionDone = SKAction.removeFromParent()
            Bullet.runAction(SKAction.sequence([action, actionDone]))

            Bullet.physicsBody = SKPhysicsBody(rectangleOfSize: Bullet.size)
            Bullet.physicsBody?.affectedByGravity = false
            Bullet.physicsBody?.dynamic = false
            self.addChild(Bullet)

        }

感谢。

Screenshot

2 个答案:

答案 0 :(得分:0)

您正在设置zPosition?你的意思是zRotation。这只会计算子弹绘制的角度,你需要做一些触发来确定子弹在直线上的位置:

PSEUDO CODE:

let action = SKAction.moveTo(
               CGPointMake(
                            travelDistance * cos(Bullet.zRotation) + Bullet.position.x,
                            travelDistance * sin(Bullet.zRotation) + Bullet.position.y
               ), 
               duration: 0.8)

答案 1 :(得分:-1)

let action = SKAction.moveTo(
                CGPointMake(
                    400 * -cos(Bullet.zRotation - 1.57079633) + Bullet.position.x,
                    400 * -sin(Bullet.zRotation - 1.57079633) + Bullet.position.y
                ),
                duration: 0.8)