1
我在iOS8中制作了一个类似于asteriods的应用程序。我的代码创建了一个激光镜头,并根据船舶的正面设置其速度矢量。当船在屏幕上朝上时,代码正常工作。当船在屏幕上向下定向时,精灵沿y轴没有或几乎没有速度。也就是说,physicsBody.velocity的.dy组件表现得好像接近零; .dx组件的行为正确。 LLDB会将.dy组件报告为应该的内容,但精灵会错误地移动。下面是相关代码:无法正确设置精灵的速度
func createLaser()->SKSpriteNode {
let laserBeam = SKSpriteNode(imageNamed: "laserBeam")
laserBeam.name = "laserBeam"
laserBeam.position.x = shipNode.position.x
laserBeam.position.y = shipNode.position.y
laserBeam.physicsBody = SKPhysicsBody(circleOfRadius: 10.0)
laserBeam.physicsBody?.dynamic = true
laserBeam.physicsBody?.affectedByGravity = false
laserBeam.zRotation = shipNode.zRotation
laserBeam.physicsBody?.mass = 1.0
laserBeam.physicsBody?.velocity.dx = kLaserSpeed * sin(shipNode.zRotation) * -1 //TODO apply an impulse instead?
laserBeam.physicsBody?.velocity.dy = kLaserSpeed * cos(shipNode.zRotation) * 1
println("in create Laser ship rotation = \(shipNode.zRotation), dx = \(laserBeam.physicsBody?.velocity.dx), dy = \(laserBeam.physicsBody?.velocity.dy)")
return laserBeam
}
不,这也不行。 – user1790252