问题
The following code recognizes the bottom and top edge of the scene and the ball bounces off as expected. However, the left and right edges of the scene are breached all the time. The ball goes off screen and then eventually returns back if enough force is applied. It is as if the edges of the scene are beyond the edges of the iphone simulator window. Can someone help? Thanks.
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView){
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
backgroundColor = UIColor.cyanColor()
var ball = SKSpriteNode(imageNamed: "ball")
ball.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(ball)
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.height/2)
let push = CGVectorMake(10, 10)
ball.physicsBody.applyImpulse(push)
}
}
回答1:
You can try to change self.physicsBody size for example;
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRectMake(self.frame.origin.x, self.frame.origin.y, self.size.width, self.size.height))
and change the parameter self.size.width
as you wish
来源:https://stackoverflow.com/questions/24894208/swift-physicsbody-edge-recognition-issue