質問

I am developing an application in which there is one object which is draggable.The user can drag this object in a specified path, not outside the path. For achieving this I had to set the boundary conditions so that if the touch point is beyond that so the object does not move.

My problem is that my object size is lets say 30*30. Now if I touch the object from its hair means at above portion and then drag it to down it gives one value, but if I touch the same object at some below position lets say his chin , then move the object at the same position where we moved the previous one.Now in this case, the the point of view are different from the previous one. So I am not able to understand what should I set the boundary conditions,if I set the conditions of first value then in his case the second touch process will not able to move, which is wrong and if I set the second case value then the first case object will able to move below the boundary also. I am not using cocos 2D.It is simple ipad application. enter image description here

For more understanding I have added the image now if you touch the green circle on his left portion and moved towards the right, the last value is like 760 but if you touch on the right portion and moved towards the right then the last points come as 780 something. same case happens if object is dragged vertically.

My basic requirement is that the object should only move inside the path shown in image not outside it.

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    // Retrieve the touch point
    CGPoint pt = [[touches anyObject] locationInView:gamePice];
   startLocation = pt;
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{

 CGPoint pt1 = [[touches anyObject] locationInView:gamePice];
 CGPoint pt = [[touches anyObject] locationInView:self.view];
   NSLog(@"moved=%f|%f",pt.x,pt.y); 

   if (pt.x>=306 && pt.y>=148 && pt.x<=720 && pt.y<=175) {  //these conditions how to set
        CGRect frame = [gamePice frame];
        frame.origin.x += pt1.x - startLocation.x;
        frame.origin.y += pt1.y - startLocation.y;
        [gamePice setFrame:frame];        
    }
}

Please suggest how to implement this feature or what approach should I follow.

役に立ちましたか?

解決

Make an outer CGRect, lets say , CGRectMake(306, 148,400,100) , its your path rect, you many need multiple rects for that, but for now start with one.

make new frame for gamePice, that will be the frame according to touch location, lets say you make it like this,

    CGRect frame = [gamePice frame];
    frame.origin.x += pt1.x - startLocation.x;
    frame.origin.y += pt1.y - startLocation.y;

then your condition will be,

 if (CGRectContainsRect(outerRect, frame) ) 
 {  
       [gamePice setFrame:frame];
 }

// else dont move...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top