1
votes

I'm working on a new game and I faced a problem (making the edges of the screen act like walls !) after some search I found this piece of code

self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)

but the problem is that only the bottom and the top of the screen are working as edges while the right and the left edges aren't working (when I throw a ball to the left it goes for a while -of the screen- then hit the edge and return !)

3
why all the answers speaks about delegate? I think your problem is not the delegate, for me you must check the size of scene, try self.view.frame or check the scale of the node - Simone Pistecchia

3 Answers

0
votes

Try this code:

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

and make sure you read over the SpriteKit Programming Guide (Listing 6-1 answers your question).

0
votes

You have to subclass your GameScene with the SKPhysicsContactDelegate in order to have collisions exists in your Scene

class GameScene: SKScene, SKPhysicsContactDelegate {
    physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)
    physicsWorld.contactDelegate = self
}

This question has already been asked and answered here: How to set contactDelegate for physicsWorld in SpriteKit?

0
votes

Check "scaleMode" of your scene. Seems it is "AspectFill" and part of your scene simply is out of view. If so - please try "AspectFit" value. Docs with explanation: https://developer.apple.com/reference/spritekit/skscenescalemode