开发者

How to check if not a point but only x or y are contained in a CGPath?

开发者 https://www.devze.com 2023-03-19 18:34 出处:网络
I\'m developing a simple game, where one can move a character through a maze. To avoid collision detecting i thought it might be a good idea to set a path through the maze and move the character along

I'm developing a simple game, where one can move a character through a maze. To avoid collision detecting i thought it might be a good idea to set a path through the maze and move the character along this path.

The character shall be moved via touch. So i thought i can just test if the touchlocation.x or touchlocation.y are contained in the path and then set the characters image on the location corresponding to either the touchlocation.x or touchlocation.y value.

Unfortunately i found out i can only test if a CGPoint is contained in a path and not if the x or the y coordinate of that point would be on that path.

Does anyone of you know how to do this?

thx in advance

Mav

----EDIT----

This is the code how i create my CGPath:

mazePath = CGPathCreateMutable();
CGPathMoveToPoint(mazePath,NULL, 148, 723);
CGPathAddLineToPoint(mazePath, NULL, 148, 404);
CGPathAddLineToPoint(mazePath, NULL, 129, 353);
CGPathAddLineToPoint(mazePath, NULL, 129, 174);
CGPathAddArcToPoint(mazePath, NULL, 129, 174, 148, 404, 10);
CGPathAddLineToPoint(mazePath, NULL, 148, 404);
CGPathAddLineToPoint(mazePath, NULL, 290, 640);
CGPathAddArcToPoint(mazePath, NULL, 290, 640, 326, 196, 10);
CGPathAddArcToPoint(mazePath, NULL, 326, 296, 359, 234, 10);
CGPathAddLineToPoint(mazePath, NULL, 395, 198);
CGPathAddLineToPoint(mazePath, NULL, 396, 147);
CGPathAddLineToPoint(mazePath, NULL, 683, 147);
CGPathAddLineToPoint(mazePath, NULL, 683, 232);
CGPathAddLineToPoint(mazePath, NULL, 480, 232);
CGPathAddLineToPoint(mazePath, NULL, 461, 332);
CGPathAddLineToPoint(mazePath, NULL, 235, 345);
CGPathAddLineToPoint(mazePath, NULL, 219, 248);
CGPathAddLineToPoint(mazePath, NULL, 235, 345);
CGPathAddLineToPoint(mazePath, NULL, 233, 607);
CGPathAddLineToPoint(mazePath, NULL, 324, 605);
CGPathAddLineToPoint(mazePath, NULL, 324, 464);
CGPathAddArcToPoint(mazePath, NULL, 324, 464, 364, 425, 10);
CGPathAddLineToPoint(mazePath, NULL, 537, 426);
CGPathAddArcToPoint(mazePath, NULL, 537, 426, 559, 396, 10);
CGPathAddLineToPoint(mazePath, NULL, 559, 396);
CGPathAddLineToPoint(mazePath, NULL, 567, 330);
CGPathAddLineToPoint(mazePath, NULL, 678, 330);
CGPathAddLineToPoint(mazePath, NULL, 678, 504);
CGPathAddLineToPoint(maz开发者_Go百科ePath, NULL, 412, 510);
CGPathAddLineToPoint(mazePath, NULL, 412, 598);
CGPathAddLineToPoint(mazePath, NULL, 691, 598);
CGPathAddLineToPoint(mazePath, NULL, 690, 723);   


i have a approach ,i think you can try this way. by using the method CGPathContainsPoint you can know whether a point is present in the path,if the condition fails it mean that the path doesn't contain the point. Now in your case you can write a condition to check whether the point is contained in the path ,and in else part develop a logic to check our point's x and y is equal to the currentPoint of the path.

CGRect box= CGPathGetBoundingBox(myPath);


if([CGPathContainsPoint:myPoint]){
// we know that the point is present in the path
}
else{

if(mypoint.x>mypoint.x && myPoint.x<=CGRectGetMaxX(box.frame))
NSLog(@"x coordinate of the point is  on the path");
if(myPoint.y>myPoint.y && myPoint.y<=CGRectGetMaxY(box.frame))
NSLog(@"y coordinate of myPoint is on the path");

}

try doing this i think it works. There might be even a better approach,try in others ways too and if u find any such plz update us.

TNQ


Have a look at this thread, it might help you NSLog with CGPoint data

0

精彩评论

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

关注公众号