I have a class CharacterCreator which makes the player and inside it I have these functions to make the player shoot:
我有一个类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
}
The playerWeaponBarrel
child node is where the bullet should spawn. What happens is that when I call the shoot() function at GameScene:
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!)
}
it just won't create the bullet at the playerWeaponBarrel. How do I make this happen?
它只是不会在playerWeaponBarrel上创建子弹。我该如何做到这一点?
1 个解决方案
#1
1
Your problem is that the playerWeaponBarrel.position
is relative to its super node, which is probably the player
. But you want the bullet
to be located absolutely in the scene, not relative to the player
. Therefore what you need to do is convert the playerWeaponBarrel.position
from its local coordinate system to the scene
coordinate system:
你的问题是playerWeaponBarrel.position是相对于它的超级节点,可能是玩家。但是你希望子弹完全位于场景中,而不是相对于玩家。因此,您需要做的是将playerWeaponBarrel.position从其本地坐标系转换为场景坐标系:
bullet.position = scene.convertPoint(playerWeaponBarrel.position, fromNode: playerWeaponBarrel.parent!)
#1
1
Your problem is that the playerWeaponBarrel.position
is relative to its super node, which is probably the player
. But you want the bullet
to be located absolutely in the scene, not relative to the player
. Therefore what you need to do is convert the playerWeaponBarrel.position
from its local coordinate system to the scene
coordinate system:
你的问题是playerWeaponBarrel.position是相对于它的超级节点,可能是玩家。但是你希望子弹完全位于场景中,而不是相对于玩家。因此,您需要做的是将playerWeaponBarrel.position从其本地坐标系转换为场景坐标系:
bullet.position = scene.convertPoint(playerWeaponBarrel.position, fromNode: playerWeaponBarrel.parent!)