【问题标题】:CGPathContainsPoint question?CGPathContainsPoint 问题?
【发布时间】:2010-02-14 23:41:21
【问题描述】:

这是在 GameViewController.m 中

-(void)fillMutablePath{



    CGPoint movePoint = CGPointFromString([pointsToFillArray objectAtIndex:0]);

    CGPathMoveToPoint(fillPath, NULL, movePoint.x, movePoint.y);
    for (int i=0; i<[pointsToFillArray count]; i++) {
        CGPoint tempPoint = CGPointFromString([pointsToFillArray objectAtIndex:i]);
        CGPathAddLineToPoint(fillPath, NULL, tempPoint.x, tempPoint.y);
        CGContextAddPath(gameViewObj._myContext, fillPath);
        CGContextFillPath(gameViewObj._myContext);

        if(CGPathContainsPoint(fillPath, nil, "What goes here?", false)){
            [self doDie];
        }
else {
  CGContextFillPath(gameViewObj._myContext);
}
        CGPathRelease(fillPath);
        [pointsToFillArray removeAllObjects];
    }


}

这是在 GameView.m 中

-(CGPoint) GetBirdPosition:(CGPoint) PtPoint :(int) BirdNumber{
    CGPoint Temp=PtPoint;
    BOOL isTouch=TRUE;
    while (isTouch)
    {
        isTouch=FALSE;
        if(playField[(int) Temp.x][(int) Temp.y]==2)
        {
            isTouch=TRUE;
            if(playField[(int) Temp.x+1][(int) Temp.y] == 2 || playField[(int) Temp.x-1][(int) Temp.y] == 2)
            {
                pos[BirdNumber].y = -pos[BirdNumber].y;
                Temp.y+=pos[BirdNumber].y;

            }
            else if(playField[(int) Temp.x][(int) Temp.y+1] == 2 || playField[(int) Temp.x][(int) Temp.y-1] == 2)
            {
                pos[BirdNumber].x = -pos[BirdNumber].x;
                Temp.x+=pos[BirdNumber].x;
            }

        }
    }
    return Temp;
}

-(void)flyingBird:(NSTimer *)timer {

    if(appDelegate.gameStateRunning == YES){

        for (int i=0; i< [birdImageViewArray count];i++)
        {
            UIImageView* birdImageView=[birdImageViewArray objectAtIndex:i];

            CGPoint Temp=CGPointMake(birdImageView.center.x + pos[i].x , birdImageView.center.y + pos[i].y);
            birdImageView.center =[self GetBirdPosition:Temp:i];

            if(playField[(int) birdImageView.center.x][(int) birdImageView.center.y]==3){

                [[NSNotificationCenter defaultCenter] postNotificationName:@"LineCrossed" object:nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"PlayDieSound" object:nil];
            }
        }
    }
}

我无法弄清楚我写“这里发生了什么”的地方发生了什么?我想要这样,如果一只鸟被困在形状内,那么 [doDie] 就会发生。如果鸟没有被困在形状中,它会被填充。任何想法都非常感谢。

【问题讨论】:

    标签: iphone sdk fill area cgpath


    【解决方案1】:

    我不太了解您的代码,但我想这基本上是您想要做的:

    CGPoint Temp=CGPointMake(birdImageView.center.x + pos[birdNum].x , birdImageView.center.y + pos[birdNum].y);
    if(CGPathContainsPoint(fillPath, nil, [myGameView GetBirdPosition:Temp:i], false)){
        [self doDie];
    }
    

    看起来您的路径最初只是一条线,我认为其中不会包含任何内容。如果您更彻底地解释目标(我们检查了多少只鸟?每只鸟的路径是否相同?)我们可以提供进一步的帮助。

    编辑:

    嗯嗯,好的。第一件事很简单。如果有鸟在路上,为了防止填充,不要使用 else case。在 if 中,如果当前鸟包含在路径中,则设置一个标志。然后在循环之后,如果没有设置标志,则填充路径。所以像:

    BOOL pathContainsBird = NO;
    for (int i=0; i<[pointsToFillArray count]; i++) {
        //bunch of stuff
        if(CGPathContainsPoint(fillPath, nil, "What goes here?", false)){
            pathContainsBird = YES;
            [self doDie];
        }
    }
    if(!pathContainsBird) {
        //fill path here
    }
    

    其次,我很确定你没有循环通过正确的事情。看起来您每次迭代都会扩展路径,但我认为您应该先构建整个路径,然后遍历所有鸟类,但我可能会误解。

    【讨论】:

    • 可能有 2-26 只鸟飞来飞去,具体取决于级别。如果其中任何一个包含在形成的形状中,我希望 [doDie] 发生。这让我疯狂。我可以得到与我想要发生的相反的事情。我只是不知道我是如何做到的,这样如果形状中包含一只鸟,该区域就不会被颜色填充。我会尝试你的建议,看看我的想法。
    • 这个填充路径是什么?到处搜索相同的问题时,我看到的是它。没有解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多