我在我的应用程序中使用精灵套件进行物理和碰撞检测。
我有一些球掉进一个盒子里。盒子的内部是使用 bodyWithPolygonFromPath 定义的
我往盒子里扔了一些球,它们直接掉了下来。
这是定义框的代码
SKSpriteNode* boxFront = [SKSpriteNode spriteNodeWithImageNamed: [boxData objectForKey: @"box_image"]];
boxFront.position = CGPointMake(0, -screenHeight*0.22);
boxFront.zPosition = 10;
[self addChild: boxFront];
CGFloat offsetX = boxFront.frame.size.width * boxFront.anchorPoint.x;
CGFloat offsetY = boxFront.frame.size.height * boxFront.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 3 - offsetX, 129 - offsetY);
CGPathAddLineToPoint(path, NULL, 0 - offsetX, 129 - offsetY);
CGPathAddLineToPoint(path, NULL, 0 - offsetX, 85 - offsetY);
CGPathAddLineToPoint(path, NULL, 200 - offsetX, 85 - offsetY);
CGPathAddLineToPoint(path, NULL, 200 - offsetX, 129 - offsetY);
CGPathAddLineToPoint(path, NULL, 197 - offsetX, 129 - offsetY);
CGPathAddLineToPoint(path, NULL, 196 - offsetX, 87 - offsetY);
CGPathAddLineToPoint(path, NULL, 3 - offsetX, 87 - offsetY);
CGPathCloseSubpath(path);
boxFront.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
boxFront.physicsBody.dynamic = NO;
SKShapeNode* shape = [[SKShapeNode alloc] init];
shape.path = path;
shape.strokeColor = [UIColor redColor];
shape.zPosition = 101;
shape.lineWidth = 2;
[boxFront addChild: shape];
SKShapeNode 的最后一位只是为了调试目的绘制了框的轮廓。绘制的框是我期望碰撞框所在的位置。
这是我放入盒子中的球的代码。
SKSpriteNode* circle1 = [SKSpriteNode spriteNodeWithImageNamed: @"ball.png"];
circle1.position = CGPointMake(0, screenHeight*0.7);
circle1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius: 20];
circle1.zPosition = 100;
[self addChild: circle1];
SKSpriteNode* circle2 = [SKSpriteNode spriteNodeWithImageNamed: @"ball.png"];
circle2.position = CGPointMake(-10, screenHeight);
circle2.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius: 20];
circle2.zPosition = 100;
[self addChild: circle2];
上面代码片段中的所有 self 实例都是直接附加到 SKScene 的同一个 SKNode。
关于什么可能导致碰撞检测被忽略的任何建议?