Dan*_*son 4 rotation sprite-kit skphysicsbody swift
所以我使用角脉冲来旋转带有物理主体的精灵。我希望屏幕的一侧向一个方向施加脉冲,而屏幕的另一侧向另一个方向施加脉冲(参见下面的代码)。
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Defines UI Touch //
let touch = touches.first as UITouch!
let touchPosition = touch.locationInNode(self)
// Left or Right movement based on touch //
if touchPosition.x < CGRectGetMidX(self.frame) {sprite.physicsBody?.applyAngularImpulse(-10)}
else {sprite.physicsBody?.applyAngularImpulse(10)}
// Smooths motion
let rate: CGFloat = 0.5; //Controls rate of motion. 1.0 instantaneous, 0.0 none.
let relativeVelocity: CGVector = CGVector(dx:angularVelocity-sprite.physicsBody!.velocity.dx, dy:0);
sprite.physicsBody!.velocity=CGVector(dx:sprite.physicsBody!.velocity.dx+relativeVelocity.dx*rate, dy:0);
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,显然如果你按屏幕左侧,然后按屏幕右侧......它会抵消脉冲并停止精灵。
我的问题是,如何更改上面的代码,使其不会取消,而是向相反方向施加全功率脉冲?
我知道通过将relativeVelocity设置为0可以轻松实现这一点,但我无法弄清楚如何在不发生“int”和“CGVector”之间冲突的情况下将该值设置为0
非常感谢任何帮助!
angularVelocity在应用相反的角脉冲之前,您可能需要设置为 0。这是例子:
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Defines UI Touch //
let touch = touches.first as UITouch!
let touchPosition = touch.locationInNode(self)
// Left or Right movement based on touch //
if touchPosition.x < CGRectGetMidX(self.frame) {
sprite.physicsBody?.angularVelocity = 0
sprite.physicsBody?.applyAngularImpulse(-10)
} else {
sprite.physicsBody?.angularVelocity = 0
sprite.physicsBody?.applyAngularImpulse(10)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
506 次 |
| 最近记录: |