0

I'm making a SpriteKit game on an iPhone 5. I'm having a weird bug where the physicsBody that is supposed to represent the edges of the screen seems to be too small, and seems to require a bizarre scaling factor to be right.

Here's what seems to be the right way to set a physicsBody to the screen dimensions:

CGRect screenRect = [[UIScreen mainScreen] bounds];
SKPhysicsBody *sBody = [SKPhysicsBody bodyWithEdgeLoopFromRect: screenRect];
[self setPhysicsBody: sBody];

But for me, this is making my other physics bodies collide with an invisible object in the middle of the screen, and making them go right off the edges of the screen without any interference.

However, this:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect x2Rect = CGRectMake(0, 0, screenRect.size.width * 1.5, screenRect.size.height * 1.5);
SKPhysicsBody *sBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:x2Rect];
[self setPhysicsBody: sBody];

Results in a complete absence of weird objects in the middle of the screen, and in other physics bodies colliding with the sides of the screen as expected.

Huh wha?

Le Mot Juiced
  • 3,761
  • 1
  • 26
  • 46
  • have you tried with: self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]; this line of code should be enough to give the screen as physicsBody – Ilario Mar 25 '14 at 17:51
  • Yep. That's what we started with. Same results as case #1. – Le Mot Juiced Mar 25 '14 at 18:12
  • 1
    where do you present the scene? If it's in viewDidLoad and app is landscape the coords will be swapped. Use viewWillLayoutSubviews to present the first scene (note: you must check for self.view.scene being nil) – CodeSmile Mar 25 '14 at 20:19
  • The app is not landscape. We are using viewDidLoad for presenting, but I don't see that it's a problem: swapped coords doesn't have anything to do with a rect's width, does it? – Le Mot Juiced Mar 25 '14 at 20:35

0 Answers0