簡體   English   中英

cocos2d使對象跟隨觸摸/手指

[英]cocos2d make object follow the touch/finger

我使用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在每次具有其他值的每個點上都不恆定。

也許是因為我有滾動背景?

您正在調用[boat runAction: [CCMoveTo actionWithDuration:1 position:location]]; 每秒多次,這將導致多個CCMoveTo操作同時運行。 這並不是設計cocos2d的Action工具的方式。

如果希望船以您定義的較慢速度跟隨觸摸,則不能將多個CCMoveTo操作排隊以響應ccTouchMoved:

而是將UITouch對象(或CGPointNSValue )推送到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];
}
- (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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM