我是SpriteKit的新手,似乎无法弄清楚如何检测两个或多个具有相同位掩码的对象之间的冲突。在我的游戏中,汽车从左/右和上/下进入场景。我需要能够跟踪汽车何时因为得分而崩溃。现在,汽车相交并崩溃(Car1从左/右Car2从顶部/底部)但不是同一车道上的汽车(Car1从左侧Car2从左侧以更高的速度)。相反,他们最终只是开车穿过彼此。这是代码:
首先我在Car.m的开头设置位掩码:
typedef enum : NSUInteger {
vehicleCategory = (1 << 0),
environmentCategory = (1 << 1),
} NodeCategory;
编辑:注意:现在我没有测试汽车和环境之间的碰撞,environmentCategory可供将来使用。
接下来是spawnCarWithColor:andSpeed:screenEdge:afterDelay:andScale:
- (Car*)spawnCarWithColor:(NSUInteger)color andSpeed:(NSUInteger)speed screenEdge:(NSUInteger)screenEdge afterDelay:(CGFloat)delay andScale:(CGFloat)carScale {
NSArray *carColorsAssets = [NSArray arrayWithObjects:@"blueCar", @"redCar", @"greenCar", nil];
carColorsForDisplay = [NSArray arrayWithObjects:@"blue", @"red", @"green", nil];
edgeOfScreenForDisplay = [NSArray arrayWithObjects:@"left", @"top", @"right", @"bottom", nil];
Car *newCar = [Car spriteNodeWithImageNamed:[carColorsAssets objectAtIndex:color]];
// set CGPath for phyics
CGFloat offsetX = newCar.frame.size.width * newCar.anchorPoint.x;
CGFloat offsetY = newCar.frame.size.height * newCar.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0 - offsetX, 24 - offsetY);
CGPathAddLineToPoint(path, NULL, 0 - offsetX, 21 - offsetY);
CGPathAddLineToPoint(path, NULL, 0 - offsetX, 18 - offsetY);
CGPathAddLineToPoint(path, NULL, 1 - offsetX, 16 - offsetY);
CGPathAddLineToPoint(path, NULL, 1 - offsetX, 13 - offsetY);
CGPathAddLineToPoint(path, NULL, 3 - offsetX, 11 - offsetY);
CGPathAddLineToPoint(path, NULL, 5 - offsetX, 8 - offsetY);
CGPathAddLineToPoint(path, NULL, 6 - offsetX, 6 - offsetY);
CGPathAddLineToPoint(path, NULL, 8 - offsetX, 5 - offsetY);
CGPathAddLineToPoint(path, NULL, 11 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 14 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 16 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 18 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 20 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 23 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 25 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 28 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 29 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 34 - offsetX, 4 - offsetY);
CGPathAddLineToPoint(path, NULL, 37 - offsetX, 4 - offsetY);
CGPathAddLineToPoint(path, NULL, 40 - offsetX, 4 - offsetY);
CGPathAddLineToPoint(path, NULL, 44 - offsetX, 4 - offsetY);
CGPathAddLineToPoint(path, NULL, 48 - offsetX, 4 - offsetY);
CGPathAddLineToPoint(path, NULL, 50 - offsetX, 4 - offsetY);
CGPathAddLineToPoint(path, NULL, 54 - offsetX, 4 - offsetY);
CGPathAddLineToPoint(path, NULL, 56 - offsetX, 4 - offsetY);
CGPathAddLineToPoint(path, NULL, 60 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 63 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 66 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 69 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 72 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 74 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 77 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 80 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 83 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 86 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 88 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 91 - offsetX, 4 - offsetY);
CGPathAddLineToPoint(path, NULL, 93 - offsetX, 6 - offsetY);
CGPathAddLineToPoint(path, NULL, 95 - offsetX, 7 - offsetY);
CGPathAddLineToPoint(path, NULL, 96 - offsetX, 10 - offsetY);
CGPathAddLineToPoint(path, NULL, 97 - offsetX, 12 - offsetY);
CGPathAddLineToPoint(path, NULL, 98 - offsetX, 15 - offsetY);
CGPathAddLineToPoint(path, NULL, 98 - offsetX, 18 - offsetY);
CGPathAddLineToPoint(path, NULL, 98 - offsetX, 21 - offsetY);
CGPathAddLineToPoint(path, NULL, 98 - offsetX, 24 - offsetY);
CGPathAddLineToPoint(path, NULL, 98 - offsetX, 26 - offsetY);
CGPathAddLineToPoint(path, NULL, 98 - offsetX, 29 - offsetY);
CGPathAddLineToPoint(path, NULL, 98 - offsetX, 32 - offsetY);
CGPathAddLineToPoint(path, NULL, 97 - offsetX, 35 - offsetY);
CGPathAddLineToPoint(path, NULL, 96 - offsetX, 39 - offsetY);
CGPathAddLineToPoint(path, NULL, 95 - offsetX, 41 - offsetY);
CGPathAddLineToPoint(path, NULL, 93 - offsetX, 44 - offsetY);
CGPathAddLineToPoint(path, NULL, 90 - offsetX, 45 - offsetY);
CGPathAddLineToPoint(path, NULL, 85 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 81 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 76 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 72 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 67 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 63 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 58 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 53 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 47 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 42 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 39 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 35 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 31 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 27 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 23 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 19 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 16 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 12 - offsetX, 46 - offsetY);
CGPathAddLineToPoint(path, NULL, 8 - offsetX, 45 - offsetY);
CGPathAddLineToPoint(path, NULL, 5 - offsetX, 43 - offsetY);
CGPathAddLineToPoint(path, NULL, 3 - offsetX, 40 - offsetY);
CGPathAddLineToPoint(path, NULL, 1 - offsetX, 35 - offsetY);
CGPathCloseSubpath(path);
newCar.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
newCar.physicsBody.contactTestBitMask = vehicleCategory;
newCar.physicsBody.collisionBitMask = vehicleCategory;
newCar.physicsBody.categoryBitMask = vehicleCategory;
newCar.physicsBody.restitution = 1.0;
newCar.physicsBody.dynamic = YES;
newCar.physicsBody.usesPreciseCollisionDetection = TRUE;
newCar.scale = carScale;
newCar.zPosition = 2;
newCar.carColor = [carColorsForDisplay objectAtIndex:color];
newCar.edgeOfScreen = [edgeOfScreenForDisplay objectAtIndex:screenEdge];
newCar.vehicleType = @"car";
switch (screenEdge) {
case 0:
newCar.zRotation = M_PI;
newCar.position = CGPointMake(-100, 512);
carVelocity = CGVectorMake(speed, 0);
leftEdgeCarTotal++;
carName = [NSString stringWithFormat:@"left%i", leftEdgeCarTotal];
[leftEdgeCars addObject:carName];
newCar.name = carName;
newCar.edgeOfScreen = @"left";
break;
case 1:
newCar.zRotation = M_PI/2;
newCar.position = CGPointMake(352, 868);
carVelocity = CGVectorMake(0, -speed);
topEdgeCarTotal++;
carName = [NSString stringWithFormat:@"top%i", topEdgeCarTotal];
[topEdgeCars addObject:carName];
newCar.name = carName;
break;
case 2:
newCar.position = CGPointMake(1124, 545);
carVelocity = CGVectorMake(-speed, 0);
rightEdgeCarTotal++;
carName = [NSString stringWithFormat:@"right%i", rightEdgeCarTotal];
[rightEdgeCars addObject:carName];
newCar.name = carName;
break;
case 3:
newCar.zRotation = -0.5*M_PI;
newCar.position = CGPointMake(415, -100);
carVelocity = CGVectorMake(0, speed);
bottomEdgeCarTotal++;
carName = [NSString stringWithFormat:@"bottom%i", bottomEdgeCarTotal];
[bottomEdgeCars addObject:carName];
newCar.name = carName;
break;
default:
break;
}
newCar.physicsBody.velocity = carVelocity;
NSLog(@"Spawned a %@ %@ from the %@, Speed of %i after a %g sec delay", [carColorsForDisplay objectAtIndex:color], newCar.vehicleType, [edgeOfScreenForDisplay objectAtIndex:screenEdge], speed, delay);
return newCar;
}
有没有办法检测两辆车在同一车道上碰撞的时间?现在我的didBeginContact非常简陋;只是一些关于节点及其属性的NSLog语句。我从这里学到了很多东西,我很激动,我现在有一个问题要问。
答案 0 :(得分:1)
根据SKPhysicsBody
的{{3}},path
方法的bodyWithPolygonFromPath:
参数描述为:
&#34; 凸多边形路径,逆时针缠绕,没有自交叉。这些点是相对于拥有节点的原点指定的。&#34;
请确保您创建的路径符合上述说明。
这里是凸多边形定义的documentation。