gpt4 book ai didi

swift - 为什么 applyImpulse 在 swift spritekit 中不起作用?

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

在我的游戏中,当用户触摸屏幕时,使用 func fire() 创建了一颗子弹,但没有任何反应。

以下是我的代码:

private func fire(position: CGPoint) {
let bullet = SKShapeNode(circleOfRadius: 2)
bullet.strokeColor = SKColor.darkGray
bullet.fillColor = SKColor.darkGray
let posX = (position.x) * -1
let posY = (position.y) * -1
let pos = CGPoint(x: posX, y: posY)
let theta = atan2((pos.y - player.position.y), (pos.x - player.position.x))
let finalPos = CGPoint(x: (player.position.x) + (CGFloat(playerRadius) * cos(theta)), y: (player.position.y) + (CGFloat(playerRadius) * sin(theta)))
bullet.physicsBody = SKPhysicsBody(circleOfRadius: 2)
bullet.position = finalPos
bullet.physicsBody!.affectedByGravity = false
bullet.physicsBody!.isDynamic = false
bullet.physicsBody!.pinned = false
numberOfBullets += 1
bullet.name = String(numberOfBullets)
bullet.physicsBody!.collisionBitMask = bodyType.enemy.rawValue
bullet.physicsBody!.categoryBitMask = bodyType.bullet.rawValue
bullet.physicsBody!.contactTestBitMask = bodyType.enemy.rawValue
bullet.physicsBody!.usesPreciseCollisionDetection = true
world.addChild(bullet)
bulletForce(bullet: bullet)
}

private func bulletForce(bullet: SKShapeNode) {
let dx = Double((bullet.position.x - player.position.x))
let dy = Double((bullet.position.y - player.position.y))
let vector = CGVector(dx: dx, dy: dy)
bullet.physicsBody!.applyImpulse(vector)
}

提前致谢。

最佳答案

它失败的原因是因为你有 isDynamic = false

isDynamic 表示可以对 body 施加力。

更多阅读请看: 阅读 developer.apple.com/reference/spritekit/skphysicsbody

关于swift - 为什么 applyImpulse 在 swift spritekit 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41859063/

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