问题
I am wondering how it is possible to create a wall with spritekit. Something at an object cannot move past. I know that I can use this code:
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
...but when I use that code I basically get a "floor" as well. I want objects to be able to pass through the bottom of the screen but not be able to leave the side.
Thanks in advance for any help!
Best Regards, Louis.
回答1:
It sounds like you need 2 physics body, one for each side of the screen. Try having something like.
// Left Wall
SKNode *node = [SKNode node];
node.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(0.0f, 0.0f, 1.0f, CGRectGetHeight(self.frame))];
[self addChild:node];
// Right wall
node = [SKNode node];
node.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(CGRectGetWidth(self.frame) - 1.0f, 0.0f, 1.0f, CGRectGetHeight(self.view.frame))];
[self addChild:node];
回答2:
You can create separate SKNodes for that.
SKNode *leftWall = [SKNode node];
leftWall.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.size.height, 1)];
leftWall.physicsBody.categoryBitMask = wallCategory;
leftWall.physicsBody.affectedByGravity = NO;
leftWall.position = CGPointMake(0, self.size.height / 2);
[self addChild:leftWall];
SKNode *rightWall = [SKNode node];
rightWall.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.size.height, 1)];
rightWall.physicsBody.categoryBitMask = wallCategory;
rightWall.physicsBody.affectedByGravity = NO;
rightWall.position = CGPointMake(self.size.width, self.size.height / 2);
[self addChild:rightWall];
来源:https://stackoverflow.com/questions/23491187/spritekit-create-a-wall