I make my first app with cocos2d, so I am very new here
my first problem:
I wont to make the object (boat) to follow my finger.
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [self convertTouchToNodeSpace: touch];
NSLog(@"location X: %f", location.x);
NSLog(@"location Y: %f", location.y);
if(startGameButtonIsPressed == YES) {
[boat runAction: [CCMoveTo actionWithDuration:1 position:location]];
}
}
It do follow but it is not fluid. If I move my finger quickly, it stops, and follow only if I stop.
The second Problem
How to calculate the distance between 2 points.
CGPoint currentLocation = ccp(boat.position.x, boat.position.y);
float distanceApart = ccpDistance(currentLocation, location);
The problem, currentLocation is not constant on each point it has every time other value…. why?
Maybe becouse I have a scrolling background??
You are calling
[boat runAction: [CCMoveTo actionWithDuration:1 position:location]];multiple times per second, which causes multipleCCMoveToactions to be running simultaneously. This is not how cocos2d’s Action tools were designed to be used.If you want the boat to follow touches at a slower speed defined by you, you cannot queue up multiple
CCMoveToactions in response toccTouchMoved:.Instead, push the
UITouchobjects (orNSValues of theCGPoints) onto anNSMutableArray. Then define a callback function to keep your boat moving after each CCMoveTo completes.Example Code: