views:

1626

answers:

2

Hi All

I have implemented a drag on a sprite object as follows..

-(BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch * touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];    
[diskSprite setPosition:ccp(location.x , location.y )];
return kEventHandled;
}

but this dragging is not smooth..... when i drag fast with my thumb the object left from the path.

Thanks

A: 

I'm not exactly sure what you mean by "the object left from the path". I suppose what you mean is that if you drag your finger over the screen in an arc or circle, that the sprite will "jump" from point to point, instead of follow your finger precisely. Is this correct?

If you want your sprite to follow an exact path, you will have to create a path and then set the sprite to follow it. What you do now is simply set the sprite's position to the touch position, but a "dragged" touch will not create an event for every pixel it touches.
It is fairly easy to create a path for touches received, and code samples can be found here and there. However, if the sprite's speed (in pixels per frame) is too high, you will always see it "jump", even if you use a smooth path.

Example:
You can animate a sprite over a circular path. If you animate this to complete the path in 1 second, you will likely see smooth animation. But if it runs at a high speed, like a full circle in 4 frames, you will just see your sprite at 4 places, not in a smooth circle. If you wish to 'correct' that, you will need to look into blending, or determine what the maximum speed is for acceptable motion, and slow your sprite down when it's too fast.

I hope that answers your question. If it's not clear, feel free to edit your question, or add a comment to my answer.

nash
Thanks for reply! My program is very simple .... i have a sprite on the screen like a square object i just want it to follow my finger when i drag my finger on the screen... (it does with my code, but not precisely) when i drag my finger fast on the screen it does not follow my finger. you can use above code in a sample project and can see what happens..... thanks again for your reply.. appreciated .....
Saurabh
+1  A: 

I had this same issue with my game. Dragging operations appeared jerky. I believe the reason is that touch events aren't generated fast enough to give a smooth appearance.

To solve the problem I smoothed the motion out by running an action on the sprite toward the desired location, instead of setting the position immediately.

Chris Garrett