简体   繁体   English

cocos2d使对象跟随触摸/手指

[英]cocos2d make object follow the touch/finger

I make my first app with cocos2d, so I am very new here 我使用cocos2d制作了第一个应用程序,所以我在这里很新

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. 如何计算2点之间的距离。

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? 问题是,currentLocation在每次具有其他值的每个点上都不恒定。

Maybe becouse I have a scrolling background?? 也许是因为我有滚动背景?

You are calling [boat runAction: [CCMoveTo actionWithDuration:1 position:location]]; 您正在调用[boat runAction: [CCMoveTo actionWithDuration:1 position:location]]; multiple times per second, which causes multiple CCMoveTo actions to be running simultaneously. 每秒多次,这将导致多个CCMoveTo操作同时运行。 This is not how cocos2d's Action tools were designed to be used. 这并不是设计cocos2d的Action工具的方式。

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: . 如果希望船以您定义的较慢速度跟随触摸,则不能将多个CCMoveTo操作排队以响应ccTouchMoved:

Instead, push the UITouch objects (or NSValue s of the CGPoint s) onto an NSMutableArray . 而是将UITouch对象(或CGPointNSValue )推送到NSMutableArray Then define a callback function to keep your boat moving after each CCMoveTo completes. 然后定义一个回调函数,以使您的船在每次CCMoveTo完成后继续前进。

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];
}
- (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. 在cocos 2d ver 2中,触摸式分派器是CCDirector的一部分,而不再是单例类。 Hence call this for the delegate functions to work. 因此,请调用此函数以使委托函数起作用。

[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM