在我的游戏中,当用户触摸屏幕时,会创建一个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)
}
提前致谢。
答案 0 :(得分:1)
失败的原因是因为你有isDynamic = false
。
isDynamic
表示可以将力量施加到身体上。
如需更多阅读,请参阅: 阅读developer.apple.com/reference/spritekit/skphysicsbody