用斯威夫特阻止一个球精灵工具包



我需要知道如何使球在两三秒后减速以施加脉冲

func setupPlayer(){
    player = SKSpriteNode(imageNamed: "ball")
    player.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    player.position = CGPoint(x: size.width/2, y: playableStart)
    let scale: CGFloat = 0.07
    player.xScale = scale
    player.yScale = scale
    player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width / 2)
    worlNode.addChild(player)
}
func movePlayer(){
    player.physicsBody?.applyImpulse((CGVectorMake( 50, 50)))
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */
    for touch in (touches as! Set<UITouch>) {
        movePlayer()
    }
}

使物理物体减速的一种方法是随时间降低其速度:

override func update(currentTime: CFTimeInterval) {
    // This controls how fast/slow the body decelerates
    let slowBy:CGFloat = 0.95
    let dx = player.physicsBody!.velocity.dx
    let dy = player.physicsBody!.velocity.dy
    player.physicsBody?.velocity = CGVectorMake(dx * slowBy, dy * slowBy)
}

你在找这样的东西吗?

let wait = SKAction.waitForDuration(2.0)
let slow = SKAction.runBlock({
    node.physicsBody?.applyImpulse(CGVectorMake(-node.physicsBody?.velocity.dx, -node.physicsBody?.velocity.dy))
    //you will probably want to mess with the vector
})
node.runAction(SKAction.sequence([wait, slow]))

相关内容

最新更新