问题
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??
回答1:
You are calling [boat runAction: [CCMoveTo actionWithDuration:1 position:location]];
multiple times per second, which causes multiple CCMoveTo
actions 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 CCMoveTo
actions in response to ccTouchMoved:
.
Instead, push the UITouch
objects (or NSValue
s of the CGPoint
s) onto an NSMutableArray
. Then define a callback function to keep your boat moving after each CCMoveTo completes.
Example Code:
//...defined elsewhere, e.g. your header file:
#define kBoatMoveTag 123
NSMutableArray *touchQueue; //treat the array like a queue.
//don't forget to alloc it before using.
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [self convertTouchToNodeSpace: touch];
NSLog(@"location X: %f", location.x);
NSLog(@"location Y: %f", location.y);
[touchQueue insertObject:[NSValue valueWithCGPoint:location] atIndex:0];
[self continueBoatMovement];
}
-(void)continueBoatMovement {
//if no queued point, or boat is already moving...
if(touchQueue.count < 1 || [boat getActionByTag:kBoatMoveTag]) {
return; //dont do anything
}
NSValue valueOfPt = [touchQueue lastObject];
[touchQueue removeLastObject];
CGPoint newPt = [valueOfPt CGPointValue];
float distance = ccpDistance(boat.position, newPt);
float duration = distance / boatSpeed; //you must define boatSpeed somewhere
CCMoveTo *move = [CCMoveTo actionWithDuration:duration position:newPt];
CCSequence *moveSeq = [CCSequence actionOne:move two:[CCCallFunc actionWithTarget:self selector:@selector(continueBoatMovement)]];
moveSeq.tag = kBoatMoveTag;
[boat runAction:moveSeq];
}
回答2:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
//Declare the lastTouchLocation as a CGPoint
lastTouchLocation = location;
}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [self convertTouchToNodeSpace: touch];
CGPoint moveBy = ccpSub(location, lastTouchLocation);
//Vary the duarion to make the sprite move slower.
[self runAction:[CCMoveBy actionWithDuration:1 position:location]];
lastTouchLocation = location;
}
In cocos 2d ver 2 the touch dispatcher is a part of the CCDirector and no more a singleton class. Hence call this for the delegate functions to work.
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
来源:https://stackoverflow.com/questions/11726218/cocos2d-make-object-follow-the-touch-finger