I have a ball (CCSprite) that move from left to right forever. When I touch the screen, I want the ball stop. The problem is if the duration of the sprite is too fast, it has a delay time a millisecond before it stop, so the stop position is almost same value even when I touch in a bit 'different' position.
CGSize winSize = [[CCDirector sharedDirector] winSize];
self.ball = [CCSprite spriteWithFile:@"ball.png"];
self.ball.position = ccp(0, 156);
CGPoint destinationPoint = self.ball.position;
destinationPoint.x = winSize.width;
CGPoint startPoint = self.ball.position;
startPoint.x = 0;
float speed = 0.8f;
[self.ball runAction:[CCRepeatForever actionWithAction:
[CCSequence actions:
[CCMoveTo actionWithDuration:speed position:destinationPoint],
[CCMoveTo actionWithDuration:0.f position:startPoint],
nil]
]];
Then when I touch the screen I want to stop the sprite, and log the ball position:
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
[self.ball stopAllActions];
NSLog(@"Ball x y: %f %f\n", self.ball.position.x, self.ball.position.y);
// Then repeat again the movement of the ball
}
So when I touch at the center, it has a delay a millisecond time before it stopped. Once again, I mean when I touch, it delay millisecond time (the ball still moving) then it stop. For example, when I click at position x 240, it stopped at x 240,1122, when I click at position x 230, it stopped at 239,222. Not immediately stop after I touch.
Here is the sample log of the position I touch.
Ball x y: 240.211624 156.000000
Ball x y: 240.369995 156.000000
Ball x y: 240.291992 156.000000
Ball x y: 219.913574 156.000000
Ball x y: 260.425598 156.000000
Ball x y: 260.004364 156.000000
Ball x y: 239.953186 156.000000
See? The x position when stop is always 'can be divided by 20'.
- 240,240,240,239
- 219
- 260,260
I'm quite sure I have ever click at position x 235,230 or 245 or 250 or anything value exclude that pattern. But like I said, there is a delay time the ball still moving, then it stop.
If I change the speed become 2.8f (or higher) for example, the ball will stop immediately, and the x y of the ball is exact position in the last position when I touch. I think it is because the duration of CCMoveTo speed is too fast.
I have tried with schedule selector method. But the time of the schedule cannot be too fast as the CCMoveTo does. So the movement using schedule will be one pixel to one pixel with duration too long time.
Is there any solution for this? Or there is such a way to implement this? Many thanks!