开发者

If statement question iphone?

开发者 https://www.devze.com 2022-12-20 15:58 出处:网络
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 bi

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 fillMutab开发者_StackOverflowlePath wouldn't take place is because a bird would be trapped within the shape. Any help would be much appreciated!!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号