Swift Sprite Kit Physics Body:球在施加脉冲后减速

Swift Sprite Kit Physics Body: Ball slows down after applying an impulse

我对 Swift 和 Sprite Kit 还很陌生!

我尝试构建一个 Pong 游戏。一切正常,但我施加脉冲的球 减速 真的很快! 我已经关闭了 gravityfriction 和 damping,并将球和场景的 restitution 设置为1.

这是我的代码:

import SpriteKit
import GameplayKit

class GameScene: SKScene {
    
  let player = SKShapeNode(rectOf: CGSize(width: 200, height: 30))
  let enemy = SKShapeNode(rectOf: CGSize(width: 200, height: 30))
  let ball = SKShapeNode(circleOfRadius: 10)

override func didMove(to view: SKView) {

let border = SKPhysicsBody(edgeLoopFrom: self.frame)
    border.restitution = 1
    border.friction = 0
    border.angularDamping = 0
    border.linearDamping = 0

    self.physicsBody = border

    scene?.physicsWorld.gravity = CGVector(dx: 0, dy: 0)

    player.position = CGPoint(x:  0, y: -(scene?.size.height)! * 0.35)
    player.fillColor = UIColor.white
    player.strokeColor = UIColor.clear
    self.addChild(player)

    player.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 200, height: 30))
    player.physicsBody?.friction = 0
    player.physicsBody?.isDynamic = false
    player.physicsBody?.angularDamping = 0
    player.physicsBody?.linearDamping = 0
    player.physicsBody?.restitution = 1
    player.physicsBody?.affectedByGravity = false

    enemy.position = CGPoint(x: 0, y: (scene?.size.height)! * 0.35)
    enemy.fillColor = UIColor.white
    enemy.strokeColor = UIColor.clear
    self.addChild(enemy)

    enemy.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 200, height: 30))
    enemy.physicsBody?.friction = 0
    enemy.physicsBody?.affectedByGravity = false
    enemy.physicsBody?.linearDamping = 0
    enemy.physicsBody?.angularDamping = 0
    enemy.physicsBody?.isDynamic = false

    ball.position = CGPoint(x: 0, y: 0)
    ball.fillColor = UIColor.white
    ball.strokeColor = UIColor.clear

    ball.physicsBody?.angularDamping = 0
    ball.physicsBody?.linearDamping = 0
    ball.physicsBody?.friction = 0
    ball.physicsBody?.restitution = 1
    ball.physicsBody?.allowsRotation = false
    ball.physicsBody?.affectedByGravity = true
    ball.physicsBody = SKPhysicsBody(circleOfRadius: 10)
    ball.physicsBody?.isDynamic = true
    self.addChild(ball)
    ball.physicsBody?.applyImpulse(CGVector(dx: -30, dy: -30))



  }

   override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {

        let pos = touch.location(in: self)

        player.position.x = pos.x

     }
  }

    override func update(_ currentTime: TimeInterval) {
         if ball.position.y >  0{
         enemy.position.x = ball.position.x
         }
    }
 }

谢谢你了!

请务必先创建物理体,然后再更改其属性

ball.physicsBody?.angularDamping = 0
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.friction = 0
ball.physicsBody?.restitution = 1
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.affectedByGravity = true
ball.physicsBody = SKPhysicsBody(circleOfRadius: 10) <----Needs to be first
ball.physicsBody?.isDynamic = true
self.addChild(ball)
ball.physicsBody?.applyImpulse(CGVector(dx: -30, dy: -30))