CGRect从孩子到自我的交集

时间:2015-09-12 19:02:29

标签: objective-c sprite-kit self cgrect cgrectintersectsrect

问题

当一个是self的孩子而另一个是sprite的孩子时,如何实现两个精灵的交集?

由于他们的立场与彼此之间的比较完全不同?

实施例;这是在每个帧之前运行以确定它们是否相交

-(void)checkInFOVWithPlayer:(Player *)player andEnemy:(Player *)enemy {
    SKNode *node = [player childNodeWithName:player.playersFOVName];

    if (CGRectIntersectsRect(node.frame, enemy.frame)) {
//        [self playerAimAtEnemy:enemy withPlayer:player];

        NSLog(@"inframe");
    } else {
        NSLog(@" ");
    }
}

但是,nodeplayer的孩子,enemyself的孩子。那么如何检查它们是否相交?

这里是他们初始化的地方

float radianAngle = ((fovAngle) / 180.0 * M_PI);

float fovOpposite = atanf(radianAngle) * fovDistance;

SKShapeNode *fov = [SKShapeNode node];
UIBezierPath *fovPath = [[UIBezierPath alloc] init];
[fovPath moveToPoint:CGPointMake(0, 0)];
[fovPath addLineToPoint:CGPointMake(fovOpposite *-1, fovDistance)];
[fovPath addLineToPoint:CGPointMake(fovOpposite, fovDistance)];
[fovPath addLineToPoint:CGPointMake(0, 0)];
fov.path = fovPath.CGPath;
fov.lineWidth = 1.0;
fov.strokeColor = [UIColor clearColor];
fov.antialiased = NO;
fov.fillColor = [UIColor greenColor];
fov.alpha = 0.2;
fov.name = @"playerFOV";
[_playerImage addChild:fov];

and enemy

NSString *bundle = [[NSBundle mainBundle] pathForResource:@"enemyImage" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
SKTexture *texture = [SKTexture textureWithImage:image];
_enemy = [Player spriteNodeWithTexture:texture];
_enemy.position = CGPointMake(100, 100);
_enemy.size = CGSizeMake(20, 20);
_enemy.playerAimAngle = [self returnRandomNumberBetween:0 to:360];
_enemy.anchorPoint = CGPointMake(0.5, 0.5);
_enemy.playerHealth = 100;
_enemy.playerIsDead = false;
[self addChild:_enemy];

1 个答案:

答案 0 :(得分:0)

-(void)checkInFOVWithPlayer:(Player *)player andEnemy:(Player *)enemy {
    SKNode *fovNode = [player childNodeWithName:player.playersFOVName];

    SKNode *node = [self childNodeWithName:@"enemy"];

    CGPoint newPosition = [self convertPoint:node.position toNode:fovNode.parent];

    if (CGRectContainsPoint(fovNode.frame, newPosition)) {
        [self playerAimAtEnemy:enemy withPlayer:player];
    }
}

这最终成为我解决问题的方法,但是,我必须找到一种方法来改变节点的框架,使其不是矩形。