我希望我的spritenode
有多个physicbodies
。我使用了bodyWithbodies
并添加了phyisicbodies
。但没有发现碰撞。
创建spritenode
的方法下方应该有更多physicbodies
。
-(void)spawnBrick
{
SKPhysicsBody *brickLeftEdge = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(brick.size.width - brick.size.width, brick.size.height - brick.size.height) toPoint:CGPointMake(brick.size.width - brick.size.width, brick.size.height)];
brickLeftEdge.categoryBitMask = brickLeftEdgeCategory;
brickLeftEdge.contactTestBitMask = leftEdgeCategory;
SKPhysicsBody *brickRightEdge = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(brick.size.width, brick.size.height - brick.size.height) toPoint:CGPointMake(brick.size.width, brick.size.height)];
brickRightEdge.categoryBitMask = brickRightEdgeCategory;
brickRightEdge.contactTestBitMask = rightEdgeCategory;
brick = [[BrickNodeOne alloc] init];
brick.name = @"Brick";
NSArray *array = [self objectForKeyedSubscript:@"Brick"];
BrickNodeOne *oldBrick;
if ([array count] > 0) {
oldBrick = [array objectAtIndex:([array count]-1)];
}
brick.position = CGPointMake(brick.size.width / 2, (brick.size.height + oldBrick.position.y)-15);
brick.physicsBody = [SKPhysicsBody bodyWithBodies:@[brickLeftEdge, brickRightEdge]];
brick.physicsBody.collisionBitMask = 0;
[self addChild:brick];
}
在DidMoveToView
中,我使用spritenode
// setup edges
_leftEdge = [[SKNode alloc] init];
// heigt + 1000 = for spawned objects above screen
_leftEdge.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(0.0, 0.0) toPoint:CGPointMake(0.0, self.size.height + 1000)];
_leftEdge.physicsBody.dynamic = NO;
_leftEdge.physicsBody.categoryBitMask = leftEdgeCategory;
_leftEdge.physicsBody.contactTestBitMask = brickLeftEdgeCategory;
_leftEdge.position = CGPointZero;
[self addChild:_leftEdge];
_rightEdge = [[SKNode alloc] init];
// heigt + 1000 = for spawned objects above screen
_rightEdge.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(self.size.width, 0.0) toPoint:CGPointMake(self.size.width, self.size.height + 1000)];
_rightEdge.physicsBody.dynamic = NO;
_rightEdge.physicsBody.categoryBitMask = rightEdgeCategory;
_rightEdge.physicsBody.contactTestBitMask = brickRightEdgeCategory;
[self addChild:_rightEdge];
以下代码包含位掩码信息:
我在头文件中声明了SKPhysicsContactDelegate
。在didMoveToView
static const uint32_t leftEdgeCategory = 0x1 << 0;
static const uint32_t rightEdgeCategory = 0x1 << 1;
static const uint32_t brickLeftEdgeCategory = 0x1 << 2;
static const uint32_t brickRightEdgeCategory = 0x1 << 3;
static const uint32_t kCarCategory = 0x1 << 4;
评论信息
所以我将physicsbody
从边缘更改为矩形,遗憾的是没有检测到任何接触。
SKPhysicsBody *brickLeftEdge = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, brick.size.height) center:CGPointMake((brick.size.width - brick.size.width) + 20 , self.size.height / 2)];
brickLeftEdge.categoryBitMask = brickLeftEdgeCategory;
brickLeftEdge.contactTestBitMask = leftEdgeCategory;
SKPhysicsBody *brickRightEdge = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, brick.size.height) center:CGPointMake(brick.size.width - 20, brick.size.height / 2)];
brickRightEdge.categoryBitMask = brickRightEdgeCategory;
brickRightEdge.contactTestBitMask = rightEdgeCategory;
答案 0 :(得分:1)
您遇到的问题与您使用静态物理实体有关。这来自关于bodyWithEdgeFromPoint: toPoint方法的文档:
边缘没有体积或质量,并且始终被视为动态 财产等于NO。 边缘可能只与基于卷的内容发生冲突 物理机构。
意味着,只有当至少一个物体被定义为动态时才会发生碰撞(默认情况下,基于体积的物理体是动态的)。