我正在尝试进行一场乒乓球比赛,但由于某些原因,球员(或球拍)和球似乎被拉到了奇怪的位置。
将动态变为假似乎有效,但我绝对需要它是动态的,让球在屏幕上反弹并让其他物理工作
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.backgroundColor = SKColor.whiteColor()
self.physicsWorld.gravity = CGVectorMake(0, 0)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody.restitution = 0.0
self.physicsBody.angularDamping = 0.0
self.physicsBody.dynamic = false
self.physicsBody.friction = 0.0
self.setUpPlayers()
self.setUpBall()
}
func setUpPlayers() {
p1 = SKSpriteNode(imageNamed: "racquet")
p1.name = "p1"
p1.position = CGPointMake(self.frame.width/2, self.frame.height*0.1)
p1.xScale = 1.5
self.addChild(p1)
p1.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size)
p1.physicsBody.dynamic = false
p1.physicsBody.categoryBitMask = pCategory
p1.physicsBody.restitution = 0.1
p1.physicsBody.usesPreciseCollisionDetection = true
p1.physicsBody.angularDamping = 0
p1.physicsBody.linearDamping = 0.0
p1.physicsBody.allowsRotation = false
p1.physicsBody.friction = 0.4
p2 = p1.copy() as SKSpriteNode
p2.position = CGPointMake(self.frame.width/2, self.frame.height*0.9)
self.addChild(p2)
}
func setUpBall() {
ball = SKSpriteNode(imageNamed: "Ball")
ball.physicsBody = SKPhysicsBody(circleOfRadius: self.frame.size.width/2)
ball.physicsBody.dynamic = true
ball.physicsBody.restitution = 1.0
ball.physicsBody.friction = 0.0
ball.physicsBody.angularDamping = 0.0
ball.physicsBody.linearDamping = 0.0
ball.physicsBody.affectedByGravity = false
ball.physicsBody.usesPreciseCollisionDetection = true
ball.position = CGPointMake(self.frame.width/2, p1.position.y+ball.frame.size.width/2+10)
self.addChild(ball)
}