0

我正在创建一个游戏,您可以在其中完成形状并填充该区域。但是,如果您的形状中有一只敌鸟,它就不会填充。我想这样做,如果您确实将一只鸟困在您的形状,你将失去生命。我怎么能写一个 if 语句,如果下面的代码没有发生,那么你就会失去生命。如果它有助于失去生命,在我的代码中称为 doDie。

-(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);
 CGPathRelease(fillPath);

 [pointsToFillArray removeAllObjects];

}

if(fillMutablePath doesn't take place when making a shape){
[self doDie];
}

就像我上面说的,fillMutablePath 不会发生的原因是因为鸟会被困在形状中。任何帮助将非常感激!!

4

2 回答 2

1

我不完全确定您如何以及在哪里检查鸟是否在路径中。我认为在填写您的路径之前您应该这样做(请参阅 if-else):

-(void)fillMutablePath{

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

   if(CGPathContainsPoint(fillPath, nil, bird.center, false)){
      [self doDie];
    }
   else {
      CGContextFillPath(gameViewObj._myContext);
    }
   CGPathRelease(fillPath);

   [pointsToFillArray removeAllObjects];
}

如果鸟在路径中死亡。否则,画。

澄清后编辑:

   //...

   CGContextAddPath(gameViewObj._myContext, fillPath);
   CGContextFillPath(gameViewObj._myContext);

   if(CGPathContainsPoint(fillPath, nil, bird.center, false)){
      [self doDie];
   }
   CGPathRelease(fillPath);
   [pointsToFillArray removeAllObjects];
}
于 2010-02-14T11:19:21.087 回答
0

该方法不能返回 BOOL 吗?

这样,如果鸟在形状中,该方法返回yes,如果鸟不在形状中,则返回no。

然后您可以稍后在 if 中使用返回值。

于 2010-02-14T22:57:58.763 回答