SKSpriteNode无法检测到与两个bodyWithEdgeLoopF​​romPath主体的冲突

时间:2015-01-30 03:19:10

标签: objective-c sprite-kit skspritenode

我使用bodyWithEdgeLoopF​​romPath创建了两个Object。但是当我通过方法检查碰撞时 - (void)didBeginContact:(SKPhysicsContact *)联系。它不起作用。

对象1:     UIBezierPath * paddleLeftPath = [UIBezierPath bezierPath];

[paddleLeftPath moveToPoint:CGPointMake(-19,17)];
[paddleLeftPath addLineToPoint:CGPointMake(20,17)];
[paddleLeftPath addLineToPoint:CGPointMake(10,6)];
[paddleLeftPath addLineToPoint:CGPointMake(17,-7)];
[paddleLeftPath addLineToPoint:CGPointMake(6,-19)];
[paddleLeftPath addLineToPoint:CGPointMake(-6,-20)];
[paddleLeftPath addLineToPoint:CGPointMake(-17,-11)];
[paddleLeftPath addLineToPoint:CGPointMake(-13,6)];

[paddleLeftPath closePath];

objectRocket = [[NodeRocket alloc] initWithWithFile:@"main0.png" scale:0.5 atPosition:CGPointMake(CGRectGetMidX(self.frame),110)];
objectRocket.name = @"Rocket";
objectRocket.physicsBody.dynamic = NO;
objectRocket.physicsBody.usesPreciseCollisionDetection = YES;
objectRocket.physicsBody.categoryBitMask = rocketCategory;
objectRocket.physicsBody.collisionBitMask =  meteorCategory;
objectRocket.physicsBody.contactTestBitMask =  meteorCategory;
objectRocket.zPosition = 10;
objectRocket.physicsBody.allowsRotation = NO;
[self addChild:objectRocket];    

对象2:     SKNode * meteorPair = [SKNode节点];         meteorPair.position = CGPointMake(0,0);         meteorPair.zPosition = 10;

    NSInteger scaleValue = (arc4random() % 8) + 1;

    meteorTexture = [SKTexture textureWithImageNamed:[NSString stringWithFormat:@"monster%d.png",(int)scaleValue]];

    [meteorTexture setFilteringMode:SKTextureFilteringNearest];

    if (pointDrop.x > self.size.width - meteorTexture.size.width/2 - 10)
    {
        pointDrop.x = self.size.width - meteorTexture.size.width/2 - 10;
    }
    else if (pointDrop.x < meteorTexture.size.width/2 + 10)
    {
        pointDrop.x = meteorTexture.size.width/2 + 10;
    }

    CGFloat x = (arc4random() % 2) + 0.5;

    CGFloat distanceToMove = self.frame.size.height - 115 - objectUFO.size.height/2;
    SKAction* moveMeteor = [SKAction moveByX:0 y:-distanceToMove duration:x/100.0 * distanceToMove];
    SKAction* removeMeteor = [SKAction removeFromParent];
    moveMeteorAndRemove = [SKAction sequence:@[moveMeteor, removeMeteor]];

    SKSpriteNode* meteorObject = [SKSpriteNode spriteNodeWithTexture:meteorTexture];
    [meteorObject setScale:0.55];
    meteorObject.position = CGPointMake(pointDrop.x, pointDrop.y - objectUFO.size.height/2 );

    UIBezierPath *paddleLeftPath = [UIBezierPath bezierPath];

    [paddleLeftPath moveToPoint:CGPointMake(-22,28)];
    [paddleLeftPath addLineToPoint:CGPointMake(21,28)];
    [paddleLeftPath addLineToPoint:CGPointMake(17,-30)];
    [paddleLeftPath addLineToPoint:CGPointMake(-18,-30)];
    [paddleLeftPath closePath];
    meteorObject.name = [NSString stringWithFormat:@"monster%d.png",(int)scaleValue];
    meteorObject.physicsBody.dynamic = NO;
    meteorObject.physicsBody.usesPreciseCollisionDetection = YES;
    meteorObject.physicsBody.categoryBitMask = meteorCategory;        
    [meteorPair addChild:meteorObject];

    [meteorPair runAction:moveMeteorAndRemove];
    meteorPair.name = @"meteor pair";

    [self addChild:meteorPair];
     meteorObject.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:paddleLeftPath.CGPath];    

请帮帮我。我做错了什么?

2 个答案:

答案 0 :(得分:0)

您是否为SKScene的physicsWorld设置了contactDelegate?也许你错过了它然后didBeginContact永远不会被调用。 在你的gameScene上尝试这个

  

self.physicsWorld.contactDelegate = self

答案 1 :(得分:0)

看起来你在分配它之前在物理主体上设置了一堆参数。

我是SKPhysics的新手,但是从所有的例子中可以看出,当你创建节点时,它会带有一个nil / null物理体。

要明确。 SKSpriteNode * meteorObject = [SKSpriteNode spriteNodeWithTexture: ... meteorObject.physicsBody.dynamic = NO; meteorObject.physicsBody.usesPreciseCollisionDetection = YES; meteorObject.physicsBody.categoryBitMask = meteorCategory; ... meteorObject.physicsBody = [SKPhysicsBody bodyWithEdgeLoopF​​romPath:

需要: -

SKSpriteNode * meteorObject = [SKSpriteNode spriteNodeWithTexture: ... meteorObject.physicsBody = [SKPhysicsBody bodyWithEdgeLoopF​​romPath: meteorObject.physicsBody.dynamic = NO; meteorObject.physicsBody.usesPreciseCollisionDetection = YES; meteorObject.physicsBody.categoryBitMask = meteorCategory; ...