2016-02-12 69 views
1

我有一個類CharacterCreator這使得玩家和裏面我有這些功能,使玩家拍攝:Swift:如何讓玩家武器生成子彈?

func shoot(scene: SKScene) { 
    if (playerArmed) { 
     let bullet = self.createBullet() 
     scene.addChild(bullet) 
     bullet.position = playerWeaponBarrel.position 
     bullet.physicsBody?.velocity = CGVectorMake(200, 0) 
    } 
} 

func createBullet() -> SKShapeNode { 
    let bullet = SKShapeNode(circleOfRadius: 2) 
    bullet.fillColor = SKColor.blueColor() 
    bullet.physicsBody = SKPhysicsBody(circleOfRadius: bullet.frame.height/2) 
    bullet.physicsBody?.affectedByGravity = false 
    bullet.physicsBody?.dynamic = true 
    bullet.physicsBody?.categoryBitMask = PhysicsCategory.Bullet 
    bullet.physicsBody?.contactTestBitMask = PhysicsCategory.Ground 
    bullet.physicsBody?.collisionBitMask = PhysicsCategory.Ground 
    return bullet 
} 

playerWeaponBarrel子節點就是子彈應產卵。會發生什麼事是,當我叫拍在GameScene()函數:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    /* Called when a touch begins */ 
    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 
     let touchedNode = self.nodeAtPoint(location) 

     if (touchedNode == fireButton) { 
      Player.shoot(self.scene!) 
     } 

它只是不會創建在playerWeaponBarrel子彈。我如何做到這一點?

回答

1

你的問題是playerWeaponBarrel.position是相對於它的超級節點,可能是player。但是您希望bullet完全位於場景中,而不是相對於player。因此,你需要做的是從本地系統轉換playerWeaponBarrel.position協調對scene座標系:

bullet.position = scene.convertPoint(playerWeaponBarrel.position, fromNode: playerWeaponBarrel.parent!) 
+0

是啊,我只是現在這樣做!謝謝您的回答 :) –