Search code examples
iosios7xcode5game-physicssprite-kit

Sprite Kit: Physics Barrier iOS7


I have a player that sits at the bottom of the screen and is controlled by the accelerometer and i have baddies that drop from the roof. I have put a physics barrier around the screen

self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

bud the baddies hit this barrier is there a way i can make them not collide with the barrier or just have a barrier either side and not on the top and bottom of the screen


Solution

  • Don't know if you solved this or not, but I was having the same problem. Baddies from the top, but needed to drop them from off screen, they kept hitting the top barrier. So what I did was to extend my SKPhysicsBody bodyWithEdgeLoopFromRect. I first made an offset to extend the top part of my rect.

    float topOffset = 547.0;
    

    This is how high above the top of the screen I made the bad guys. Then I created my new frame with.

    CGRect newFrame = CGRectMake(0, -topOffset, self.frame.size.width, self.frame.size.height + topOffset);
    

    Notice that for the height I had to add the topOffset so it wouldn't crash! Then I just applied the bodyWithEdgeLoopFromRect.

    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:newFrame];
    

    This still leaves a bottom boundary, but you could extend that one as well if you wish. Hope this helps someone, it took me a while to figure it out!!