0

我用 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]];
    }
} 

它确实跟随,但它不是流动的。如果我快速移动手指,它就会停止,并且只有在我停止时才会跟随。

第二个问题

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

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

问题是,currentLocation 在每个点上都不是恒定的,它每次都有其他值......为什么?

也许是因为我有滚动背景?

4

2 回答 2

1

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

如果您希望小船以您定义的较慢速度跟随触摸,则您不能将多个动作排队CCMoveTo以响应ccTouchMoved:

相反,将UITouch对象(或NSValues 的CGPoints)推到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];
}
于 2012-07-30T18:23:58.000 回答
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];
于 2013-09-17T10:57:04.133 回答