gpt4 book ai didi

ios - Swift:如何让子弹在玩家武器上产生?

转载 作者:行者123 更新时间:2023-11-28 06:49:21 25 4
gpt4 key购买 nike

我有一个创建玩家的 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!)

关于ios - Swift:如何让子弹在玩家武器上产生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35369349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com