2016-07-04 284 views
0

我嘗試構建2D - 自上而下的遊戲,並且我有玩家(SKSpriteNode)他可以移動和旋轉,並且我想從他身上射出兩顆子彈。 我用這個代碼來拍攝:SKSpriteNode射擊子彈

func setBullet(player: Player, bullet: Int) 
{ 
    let weaponPosition = scene!.convertPoint(player.weapon.position, fromNode: player) 
    var xPos, yPos: CGFloat! 

    let sinus = sin(player.zRotation) 
    let cosinus = cos(player.zRotation) 

    if bullet == 1 
    { 
     xPos = converted.x - sinus * player.size.height/2 
     yPos = converted.y + cosinus * player.size.height/2 
    } 
    else if bullet == 2 
    { 
     xPos = weaponPosition.x - sinus * player.size.height/2 
     yPos = weaponPosition.y + cosinus * player.size.height/2 
    } 

    position = CGPoint(x: xPos, y: yPos) 
    physicsBody!.applyImpulse(CGVector(dx: -sinus * normalSpeed, dy: cosinus * normalSpeed)) 
} 

但是,我不知道如何正確設置位置... 我儘量讓這樣的事情this (綠點 - 這是一個子彈)。任何人都可以幫助我!

回答

1

在同一個方向上拍攝多顆子彈相當簡單。關鍵是確定角色旋轉時子彈的初始位置和方向矢量。

您可以通過

let point = node.convertPoint(weapon.position, toNode: self) 

計算場景內的子彈的初始位置node就是人品,weapon.position是槍的非旋轉位置,並且self是場景。

通常,當角色未旋轉時,子彈向右移動CGVector(dx:1, dy:0)或向上移動CGVector (dx:0, dy:1)。你可以計算的衝動方向旋轉通過人物的zRotation向量與

func rotate(vector:CGVector, angle:CGFloat) -> CGVector { 
    let rotatedX = vector.dx * cos(angle) - vector.dy * sin(angle) 
    let rotatedY = vector.dx * sin(angle) + vector.dy * cos(angle) 
    return CGVector(dx: rotatedX, dy: rotatedY) 
} 

適用於子彈的物理身體下面是如何從旋轉的字符拍兩發子彈的例子:

struct Weapon { 
    var position:CGPoint 
} 

class GameScene: SKScene { 
    let sprite = SKSpriteNode(imageNamed:"Spaceship") 
    let dualGuns = [Weapon(position: CGPoint(x: -15, y: 15)), Weapon(position: CGPoint(x: 15, y: 15))] 
    let singleGun = [Weapon(position: CGPoint(x: 0, y: 15))] 
    let numGuns = 1 
    // If your character faces up where zRotation == 0, offset by pi 
    let rotationOffset = CGFloat(M_PI_2) 
    override func didMoveToView(view: SKView) { 
     scaleMode = .ResizeFill 
     sprite.position = view.center 
     sprite.size = CGSizeMake(25, 25) 
     self.addChild(sprite) 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     if let _ = touches.first { 
      let action = SKAction.rotateByAngle(CGFloat(M_PI_4/2), duration:1) 
      sprite.runAction(action) { 
       [weak self] in 
       if let scene = self { 
        switch (scene.numGuns) { 
        case 1: 
         for weapon in scene.singleGun { 
          scene.shoot(weapon: weapon, from: scene.sprite) 
         } 
        case 2: 
         for weapon in scene.dualGuns { 
          scene.shoot(weapon: weapon, from: scene.sprite) 
         } 
        default: 
         break 
        } 
       } 
      } 
     } 
    } 

    func shoot(weapon weapon:Weapon, from node:SKNode) { 
     // Convert the position from the character's coordinate system to scene coodinates 
     let converted = node.convertPoint(weapon.position, toNode: self) 

     // Determine the direction of the bullet based on the character's rotation 
     let vector = rotate(CGVector(dx: 0.25, dy: 0), angle:node.zRotation+rotationOffset) 

     // Create a bullet with a physics body 
     let bullet = SKSpriteNode(color: SKColor.blueColor(), size: CGSizeMake(4,4)) 
     bullet.physicsBody = SKPhysicsBody(circleOfRadius: 2) 
     bullet.physicsBody?.affectedByGravity = false 
     bullet.position = CGPointMake(converted.x, converted.y) 
     addChild(bullet) 
     bullet.physicsBody?.applyImpulse(vector) 
    } 

    // Rotates a point (or vector) about the z-axis 
    func rotate(vector:CGVector, angle:CGFloat) -> CGVector { 
     let rotatedX = vector.dx * cos(angle) - vector.dy * sin(angle) 
     let rotatedY = vector.dx * sin(angle) + vector.dy * cos(angle) 
     return CGVector(dx: rotatedX, dy: rotatedY) 
    } 
} 
+0

謝謝,但你能寫代碼只是爲了正確的位置,而不是向量? – DonMugad

+0

我更新我的代碼! – DonMugad

+0

我將現在檢查它:) – DonMugad

1

假設您的播放器是一個與SKShapeNodeSKSpriteNode對齊的圓圈。

他們都具有frame屬性:

let f = player.frame 

所以,第一顆子彈可以在這個位置:

let firstBullet.position = CGPointMake(player.position.x-(f.width/2),player.position.y) 
let secondBullet.position = CGPointMake(player.position.x+(f.width/2),player.position.y) 

要旋轉的過程中知道這事:

let firstBulletXPos = firstBullet.position.x - sinus * bullet.size.height/2 
let firstBulletYPos = firstBullet.position.y + cosinus * bullet.size.height/2 

let secondBulletXPos = secondBullet.position.x - sinus * bullet.size.height/2 
let secondBulletYPos = secondBullet.position.y + cosinus * bullet.size.height/2 
+0

謝謝,但我的玩家可以旋轉和子彈位置不正確! – DonMugad

+0

現在呢? –

+0

它不工作:( – DonMugad