Reputation: 1000
In order to define an rectangular edge, this is the code I wrote:
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
SKNode *edge = [SKNode node];
edge.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
[self addChild:edge];
}
I want this edge to wrap the whole screen, i.e bottom, top, left and right borders.
I wish that the objects that I add to my scene bounce on all borders. But, those objects only bounce on bottom and top part of the edge.
P.S: The same code, worked about a year ago when SpriteKit
GameScene.m
class had -(id)initWithSize:(CGSize)size
instead of -(void)didMoveToView:(SKView *)view
.
Upvotes: 2
Views: 79
Reputation: 13675
Currently SpriteKit by default loads a scene from .sks file and by default, the scene size is set to 1024x768. That is probably why you are getting unexpected results (self.frame
has different size in compare to a view).
There are few things you should keep in mind these days:
When scene is loaded from .sks file, initWithSize
: is never called. initWithCoder: is called instead. If you want to use initWithSize
: you should create a scene in "old" way - using sceneWithSize:
In initWithSize
: the view is always nil, so all the code which requires a view, should be moved to didMoveToView
.
In viewDidLoad
a final size of a view may not be known yet. A proper implementation of viewWillLayoutSubviews
can be used to get around this. Read more here.
What I would suggest you, is to create a scene inside viewWillLayoutSubviews
using sceneWithSize
: method and initialize it with view.bounds.size
. After that, you can create borders like this:
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
Hint: It can be useful to enable visual physics representation. You can do this from view controller:
skView.showsPhysics = YES;
If all this doesn't help, check if your view has correct size. View can be wrongly sized if wrong launch images are supplied.
Hope this helps and make sense.
Upvotes: 3