2012-12-14 41 views
1

我正在製作一個自頂向下的基於瓷磚的遊戲(在GameBoy上考慮老的口袋妖怪和塞爾達遊戲)。我在角色移動過程中遇到問題。我認爲問題在於完成一項行動和開始一項新行動之間的延遲。Cocos2d - 角色似乎是口吃步進

下面的代碼是什麼樣子:

- (BOOL)ccTouchBegan:(UITouch *)觸摸withEvent:方法(*的UIEvent)事件{ CGPoint touchCoor = [自我coordinateForTouch:觸摸]。

// If the character was touched, open their dialogue 
if (CGPointEqualToPoint(touchCoor, ebplayer.coordinate)) { 
    [[CCDirector sharedDirector] replaceScene: 
    [CCTransitionMoveInR transitionWithDuration:1.0 scene:[HelloWorldLayer scene]]]; 
} 
else // otherwise, move the character 
{ 
    activeTouch = [self directionForPoint:[touch locationInView:[touch view]]]; 
    [self movePlayer:nil inDirection:activeTouch]; 
} 

return YES; 

}

//給定在屏幕尺寸點 //對所有運動 基座運動功能 - (無效)movePlayer:(的NSString *)PID toPosition:(CGPoint)位置{ CGPoint playerCoordinate = [self coordinateForPositionPoint:position];

// if we're not already moving, and we can move, then move 
if(!isAnimating && [self coordinateIsOnMap:playerCoordinate] && [self isPassable:playerCoordinate]){ 
    id doneAction = [CCCallFuncN actionWithTarget:self selector:@selector(finishedAnimating)]; 
    id moveAction = [CCMoveTo actionWithDuration:WALK_DURATION position:position]; 
    id animAction = [CCAnimate actionWithAnimation: [ebplayer animateDirection:activeTouch withDuration:WALK_DURATION]]; 
    id walkAndMove = [CCSpawn actionOne:moveAction two:animAction]; 
    id action = [CCSequence actions: walkAndMove, doneAction, nil]; 
    isAnimating = YES; 
    [player runAction:action]; 

    ebplayer.coordinate = playerCoordinate; 
    [self setViewpointCenter:position Animated:YES]; 
} 

// if it's not passable, just run the animation 
if(!isAnimating){ 
    id doneAction = [CCCallFuncN actionWithTarget:self selector:@selector(finishedAnimating)]; 
    id animAction = [CCAnimate actionWithAnimation: [ebplayer animateDirection:activeTouch withDuration:WALK_DURATION]]; 
    id action = [CCSequence actions: animAction, doneAction, nil]; 
    isAnimating = YES; 
    [player runAction: action]; 
} 

}

然後當動作結束時,嘗試再次啓動它:

  • (無效)finishedAnimating { isAnimating = NO; [self movePlayer:nil inDirection:activeTouch]; }

回答

0

當您對多個CCMove *動作進行排序時,您總是會得到1幀的延遲。

所發生的是以下內容:

  • 幀0-100:移動動作運行時,子畫面移動
  • 框架101:移動動作結束時,運行CCCallFunc
  • 框架102:新的移動動作開始

這種一幀延遲是排序移動操作的主要問題之一,也是我不推薦使用移動操作進行遊戲操作的原因之一。

另一種方法是通過修改它們的位置來以預定的更新方法手動移動對象。您可以使用CCMove *操作碼作爲基礎。

+0

只是要清楚,做一些事情:onUpdateEvent - if(haventArrived){moveSomeDistance} – InkGolem

+0

此外,是不是可以動態排隊更多的行動?如同在另一個完成之前添加額外的移動動作一樣? – InkGolem