Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spritekit - Create a "wall"

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.

like image 281
user3578149 Avatar asked Apr 14 '26 14:04

user3578149


2 Answers

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];
like image 55
Guy Kogus Avatar answered Apr 17 '26 04:04

Guy Kogus


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];
like image 44
ZeMoon Avatar answered Apr 17 '26 03:04

ZeMoon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!