gpt4 book ai didi

ios - 无法正确设置 Sprite 的速度

转载 作者:行者123 更新时间:2023-11-28 09:08:51 25 4
gpt4 key购买 nike

我正在 iOS8 中制作一个类似小行星的应用程序。我有创建激光射击并根据船的朝向设置其速度矢量的代码。当船在屏幕上朝上时,代码可以正常工作。当船在屏幕上朝向下方时, Sprite 没有或几乎没有沿 y 轴的速度。也就是说,physicsBody.velocity 的 .dy 组件表现得好像接近于零; .dx 组件行为正确。 LLDB 报告 .dy 组件应该是什么,但 Sprite 移动不正确。相关代码如下:

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
}

最佳答案

试试这个:

laserBeam.physicsBody?.velocity.dx = kLaserSpeed * cos(shipNode.zRotation)
laserBeam.physicsBody?.velocity.dy = kLaserSpeed * sin(shipNode.zRotation)

关于ios - 无法正确设置 Sprite 的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30065759/

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