Spritekit - Create a “wall”

不打扰是莪最后的温柔 提交于 2019-12-06 02:46:59

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];

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!