暂停 SKSpriteNode(Swift,SpriteKite)
Pausing SKSpriteNode (Swift, SpriteKite)
我需要暂停我的比赛。我想让它们停在原地,但它们是靠冲动移动的,如果我让它们变得非动态来阻止它们,冲动就会消失。我试图暂停它们并取消暂停它们,但它们仍然朝着同一个方向前进。我试过了
ball.paused = true
但这没有用。有人知道吗?
您可以使用 ball.physicsBody.velocity = CGVectorMake(0, 0);
但请记住,如果球受到重力影响,它仍然会掉落。
我没有冻结节点,而是冻结了场景
场景?.physicsWorld.speed = 0
因为这会冻结所有节点,而不是我的时间或分数整数,这正是我所需要的。
以下是我在游戏中的解决方法:
var ballVelocity = CGVector()
func pauseAction(ball: SKSpriteNode){
if ball.dynamic {
//Ball is moving. Save the current velocity of ball.
ballVelocity = ball.velocity
//Stop the ball
ball.physicsBody.dynamic = false
}else{
//Ball is paused.
//Resume ball movement.
ball.physicsBody.dynamic = true
//Add the saved velocity.
ball.velocity = ballVelocity
}
}
我需要暂停我的比赛。我想让它们停在原地,但它们是靠冲动移动的,如果我让它们变得非动态来阻止它们,冲动就会消失。我试图暂停它们并取消暂停它们,但它们仍然朝着同一个方向前进。我试过了
ball.paused = true
但这没有用。有人知道吗?
您可以使用 ball.physicsBody.velocity = CGVectorMake(0, 0);
但请记住,如果球受到重力影响,它仍然会掉落。
我没有冻结节点,而是冻结了场景 场景?.physicsWorld.speed = 0 因为这会冻结所有节点,而不是我的时间或分数整数,这正是我所需要的。
以下是我在游戏中的解决方法:
var ballVelocity = CGVector()
func pauseAction(ball: SKSpriteNode){
if ball.dynamic {
//Ball is moving. Save the current velocity of ball.
ballVelocity = ball.velocity
//Stop the ball
ball.physicsBody.dynamic = false
}else{
//Ball is paused.
//Resume ball movement.
ball.physicsBody.dynamic = true
//Add the saved velocity.
ball.velocity = ballVelocity
}
}