简体   繁体   English

Spritekit - 创建“墙”

[英]Spritekit - Create a “wall”

I am wondering how it is possible to create a wall with spritekit. 我想知道如何用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. 最好的问候,路易斯。

It sounds like you need 2 physics body, one for each side of the screen. 听起来你需要2个物理体,屏幕的每一面都有一个。 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。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM