斯威夫特:如何在玩家武器上制造子弹?

时间:2016-02-12 18:05:21

标签: ios swift sprite-kit

我有一个类CharacterCreator让玩家在其内部我有这些功能让玩家拍摄:

$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($ProjectCollectionUri);
        #Get Work Item Store object
        $wiStore = $teamProjectCollection.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
        $teamProject = $wiStore.Projects["$ProjectName"]

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 } 子节点是子弹应该生成的位置。当我在GameScene上调用shoot()函数时会发生什么:

playerWeaponBarrel

它只是赢得了在playerWeaponBarrel创建子弹。我该如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

您的问题是playerWeaponBarrel.position与其超级节点相关,可能是player。但是您希望bullet绝对位于场景中,而不是相对于player。因此,您需要做的是将playerWeaponBarrel.position从其本地坐标系转换为scene坐标系:

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