cocos2d使对象跟随触摸/手指

时间:2012-07-30 17:06:45

标签: ios cocos2d-iphone

我用cocos2d创建了我的第一个应用程序,所以我在这里很新

我的第一个问题:

我不会让对象(船)跟着我的手指。

-(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]];
    }
} 

它确实遵循但不流畅。如果我快速移动手指,它会停止,只有在我停下来时才会跟随。

第二个问题

如何计算2点之间的距离。

CGPoint currentLocation = ccp(boat.position.x, boat.position.y);    
float distanceApart = ccpDistance(currentLocation, location);

问题是,currentLocation在每次有其他值时都不是常数....为什么?

也许因为我有滚动背景?

2 个答案:

答案 0 :(得分:1)

您每秒多次调用[boat runAction: [CCMoveTo actionWithDuration:1 position:location]];,这会导致多个CCMoveTo操作同时运行。这不是 cocos2d的动作工具的设计方式。

如果您希望船只以您定义的较慢速度跟踪触摸,则无法排队多个CCMoveTo操作以响应ccTouchMoved:

而是将UITouch个对象(或NSValue s的CGPoint)推送到NSMutableArray。然后定义一个回调函数,以便在每次CCMoveTo完成后保持船只移动。

示例代码:

//...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];
}

答案 1 :(得分:0)

- (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;
}

在cocos 2d ver 2中,触摸调度程序是CCDirector的一部分,不再是单例类。因此,调用此方法可以使委托函数起作用。

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