views:

212

answers:

2

I am creating a game where where you complete shapes and the area gets filled in. However, if there is an enemy bird within your shape, it will not fill in. I want to make it so that if you do trap a bird within your shape, you will lose a life. How can I write an if statement that pretty much says if the below code doesn't take place, then you lose a life. If it helps losing a life is called doDie in my code.

-(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];
}

Like i said above, the reason fillMutablePath wouldn't take place is because a bird would be trapped within the shape. Any help would be much appreciated!!

+1  A: 

I'm not entirely sure how and where you check if the bird is in the path. I think that right before filling you path you should do (see that 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];
}

If the bird is in the path die. Else, draw.

Edit after the clarification:

   //...

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

   if(CGPathContainsPoint(fillPath, nil, bird.center, false)){
      [self doDie];
   }
   CGPathRelease(fillPath);
   [pointsToFillArray removeAllObjects];
}
Dimitris
it draws no matter what, i need to check if the bird is in the shape after it's complete
NextRev
ok, see my updated answer. Hope that's what you're looking for.
Dimitris
the problem is that the bird center is defined in another file
NextRev
Well, you will have to find a way of getting that information into this method to do the check. The way to achieve that is beyond the scope of this question though.
Dimitris
A: 

Could the method not return a BOOL?

That way if the bird is in the shape, the method returns yes, and if the bird is not in the shape, it returns no.

Then you can use the return value in an if later on.

Jasarien
the problem is that the bird point is defined in another file. i just posted another question with more information about the problem. this is the last thing i need to figure out to complete my app
NextRev