我有一个CharacterCreator类,它使播放器起作用,并且在其中具有使播放器射击的以下功能:
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中调用shoot()函数时: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上创建项目符号。我如何做到这一点?
最佳答案
您的问题是playerWeaponBarrel.position
是相对于其 super 节点的,这可能是player
。但是,您希望bullet
绝对位于场景中,而不是相对于player
。因此,您需要做的是将playerWeaponBarrel.position
从其本地坐标系转换为scene
坐标系:
bullet.position = scene.convertPoint(playerWeaponBarrel.position, fromNode: playerWeaponBarrel.parent!)