cocos2d使对象跟随触摸/手指 [英] cocos2d make object follow the touch/finger
问题描述
我用cocos2d做我的第一个应用程序,所以我在这里很新
I make my first app with cocos2d, so I am very new here
我的第一个问题:
我不会让物体(船)顺着我的手指。
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.
第二个问题
如何计算2个点之间的距离。
How to calculate the distance between 2 points.
CGPoint currentLocation = ccp(boat.position.x, boat.position.y);
float distanceApart = ccpDistance(currentLocation, location);
问题是,currentLocation在每次具有其他值时的每个点上都不恒定....为什么
The problem, currentLocation is not constant on each point it has every time other value.... why?
也许是因为我有滚动背景?
Maybe becouse I have a scrolling background??
推荐答案
您正在呼叫 [boat runAction:[CCMoveTo actionWithDuration:1 position:location]];
每秒多次,这会导致多个 CCMoveTo
要同时运行的动作。这不是设计使用 cocos2d的操作工具的方式。
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.
如果您希望船以您定义的较慢速度跟随触摸,则不能排队多个 CCMoveTo
响应 ccTouchMoved:
的操作。
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:
.
相反,按下 UITouch
对象(或 NSValue
的 CGPoint
到 NSMutableArray
。然后定义一个回调函数,以使您的船在每次CCMoveTo完成后继续前进。
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.
示例代码:
//...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];
}
这篇关于cocos2d使对象跟随触摸/手指的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!