【问题标题】:Stop a ball sprite kit with Swift使用 Swift 停止球精灵套件
【发布时间】:2015-08-03 15:30:28
【问题描述】:

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

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()
    }
}

【问题讨论】:

    标签: swift sprite-kit skphysicsbody


    【解决方案1】:

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

    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)
    }
    

    【讨论】:

      【解决方案2】:

      你在寻找这样的东西吗?

      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]))
      

      【讨论】:

        猜你喜欢
        • 2015-12-14
        • 1970-01-01
        • 2014-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多