如何制作一个不会不断加速物体的力 SpriteKit Swift
How to make a force that doesn't accelerate objects constantly SpriteKit Swift
所以我想制造一个力来移动场景中的物理对象,而不是不断地加速它们。就像在几何破折号中一样。所以当我使用重力时就像下落一样,但我不希望它像那样加速。
希望你能理解,解释起来有点棘手。
谢谢
在 SKPhysicsBody 术语中,这是 force
(持续应用)和 impulse
(即时应用)之间的区别:
You can choose to apply either a force or an impulse:
A force is applied for a length of time based on the amount of simulation time that passes between when you apply the force and when the next frame of the simulation is processed. So, to apply a continuous force to an body, you need to make the appropriate method calls each time a new frame is processed. Forces are usually used for continuous effects
An impulse makes an instantaneous change to the body’s velocity that is independent of the amount of simulation time that has passed. Impulses are usually used for immediate changes to a body’s velocity.
(来自 'Making Physics Bodies Move' 来自 Apple documentation of SKPhysicsBody。)
因此,要对您的 body 施加脉冲,从而避免持续加速,请使用方法 applyImpulse。
在您的游戏场景中,添加一个从 touchesBegan
调用的新 jump()
方法。使用 touchesBegan
和 touchesEnded
设置一个 bool 来跟踪您触摸屏幕的时间。您可以使用场景的 didBegin
方法来跟踪玩家何时降落在地面上(或与障碍物发生碰撞等)。
像这样:
class GameScene: SKScene, SKPhysicsContactDelegate {
var person = SKSpriteNode()
var isJumping = Bool()
var isTouching = Bool()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
isTouching = true
jump()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
isTouching = false
}
func jump(){
if isTouching == true {
if isJumping == false {
person.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 600))
isJumping = true
}
}
}
func didBegin(_ contact: SKPhysicsContact) {
let bodyA = contact.bodyA.node
let bodyB = contact.bodyB.node
// Collided with ground
if bodyA?.physicsBody?.categoryBitMask == 2 && bodyB?.physicsBody?.categoryBitMask == 1 {
isJumping = false
jump()
}
}
}
所以我想制造一个力来移动场景中的物理对象,而不是不断地加速它们。就像在几何破折号中一样。所以当我使用重力时就像下落一样,但我不希望它像那样加速。
希望你能理解,解释起来有点棘手。 谢谢
在 SKPhysicsBody 术语中,这是 force
(持续应用)和 impulse
(即时应用)之间的区别:
You can choose to apply either a force or an impulse:
A force is applied for a length of time based on the amount of simulation time that passes between when you apply the force and when the next frame of the simulation is processed. So, to apply a continuous force to an body, you need to make the appropriate method calls each time a new frame is processed. Forces are usually used for continuous effects
An impulse makes an instantaneous change to the body’s velocity that is independent of the amount of simulation time that has passed. Impulses are usually used for immediate changes to a body’s velocity.
(来自 'Making Physics Bodies Move' 来自 Apple documentation of SKPhysicsBody。)
因此,要对您的 body 施加脉冲,从而避免持续加速,请使用方法 applyImpulse。
在您的游戏场景中,添加一个从 touchesBegan
调用的新 jump()
方法。使用 touchesBegan
和 touchesEnded
设置一个 bool 来跟踪您触摸屏幕的时间。您可以使用场景的 didBegin
方法来跟踪玩家何时降落在地面上(或与障碍物发生碰撞等)。
像这样:
class GameScene: SKScene, SKPhysicsContactDelegate {
var person = SKSpriteNode()
var isJumping = Bool()
var isTouching = Bool()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
isTouching = true
jump()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
isTouching = false
}
func jump(){
if isTouching == true {
if isJumping == false {
person.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 600))
isJumping = true
}
}
}
func didBegin(_ contact: SKPhysicsContact) {
let bodyA = contact.bodyA.node
let bodyB = contact.bodyB.node
// Collided with ground
if bodyA?.physicsBody?.categoryBitMask == 2 && bodyB?.physicsBody?.categoryBitMask == 1 {
isJumping = false
jump()
}
}
}