我有以下代码模拟弹跳球。虽然我提到身体应该保持在自我框架内,但它们会离开(整个球不再可见),然后再回来。因为框架的宽度大于可见边框的宽度。但是,身高完全没法。
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, -9.8)
let sceneBody = SKPhysicsBody(edgeLoopFromRect: frame)
sceneBody.friction = 0
self.physicsBody = sceneBody
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches{
var pos = touch.locationInNode(self)
var ball = SKShapeNode(circleOfRadius: 25)
ball.position = pos
ball.physicsBody = SKPhysicsBody(circleOfRadius: 25)
ball.physicsBody?.affectedByGravity = true
ball.physicsBody?.restitution = 0.9
ball.physicsBody?.linearDamping = 0
self.addChild(ball)
}
}