问题
I implemented a simple physics to bounce a sprite node
, and everything worked except the node is bouncing off the screen for the top and bottom.It is fine for the sides, but it goes off the screen for top and bottom view.
Here is the view:
As you can see, I the ball is going off the screen at the bottom, where it perefectly touches the other three sides.
This is because, I made a change to:
self.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height-100)];
If I use the scene
frame
, then the ball goes off the screen at both top and bottom, but it bounce though.
self.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
My question is why self.frame
is not enough to set the bounds for the scene
physicsBody
?
The only condition worked for me when I set the bounds like this:
self.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(self.frame.origin.x, self.frame.origin.y+100, self.frame.size.width, self.frame.size.height-200)];
回答1:
This is because the scene's size and the view's size are not the same.
Add this line in the GameViewController.m file :
scene.size = skView.bounds.size
It goes here :
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
scene.size = skView.bounds.size
Ofcourse, you need to position the view normally (remove the +100 etc that you have added).
来源:https://stackoverflow.com/questions/34048916/how-to-set-bounds-to-scene-physics-body-to-bounce-properly