让我的游戏对象永远弹跳?

时间:2017-11-16 15:19:33

标签: swift game-physics

所以我试图打乒乓球比赛,我能够让球移动并弹跳,但球速度减慢。是什么代码保持球速度恒定?这就是我得到的:

  var ball = SKSpriteNode()
var enemy = SKSpriteNode()
var main = SKSpriteNode()

override func didMove(to view: SKView) {



    ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width)
    ball = self.childNode(withName: "ball") as! SKSpriteNode
    enemy = self.childNode(withName: "enemy") as! SKSpriteNode
    main = self.childNode(withName: "main") as! SKSpriteNode

    ball.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 20))

    let border = SKPhysicsBody(edgeLoopFrom: self.frame)

    border.friction = 0
    border.restitution = 1

    self.physicsBody = border

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        main.run(SKAction.moveTo(x: location.x, duration: 0.2))
    }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        main.run(SKAction.moveTo(x: location.x, duration: 0.2))
    }
}


  override  func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered

        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 1.0))
}

}

我的物理设置设置如下:

physics definition

提前致谢!

0 个答案:

没有答案