由于跟踪,Sprite具有跳汰效果

时间:2012-05-27 19:19:29

标签: iphone ios ipad cocos2d-iphone

我有一个精灵跟踪并跟踪屏幕上的物体,向它们移动。

该方法按计划运行,基本上如下所示:

- (void) nextFrame:(ccTime)dt {
    //calculate distance between the bubble and the fish
    float dx = bubbleToChase.position.x - fish.position.x;
    float dy = bubbleToChase.position.y - fish.position.y;
    float d = sqrt(dx*dx + dy*dy);
    float v = 400;

    if (d >  190){
        NSLog(@"moving the fish!");
        fish.position = ccp( fish.position.x + dx/d * v *dt, 
                            fish.position.y + dy/d * v *dt);

    }
}

代码效果很好,当距离大于190时,鱼会游向它。

问题在于物体具有物理特性,因此它们在屏幕上滑动。这会对鱼精灵产生跳汰/晃动效应,因为鱼一旦到达气泡就会停止,但随着气泡逐渐消失(d> 190),它会快速摇晃并停止。

如何摆脱这种跳汰效应?一旦它到达泡沫的位置,我想阻止它移动。或任何可以平滑的替代方案。任何帮助表示感谢,谢谢。

1 个答案:

答案 0 :(得分:0)

if (d >  190 && !fish.parked){
    NSLog(@"moving the fish!");
    fish.position = ccp( fish.position.x + dx/d * v *dt, 
                        fish.position.y + dy/d * v *dt);

}else{
   if( fish.parked ){
   // what you want to do while parked
   // just sit there, wander randomly, 
   // then unpark the fish...
   if(unpark)
      fish.parked=FALSE;
   }else{
     fish.parked=TRUE;
     // set variables for parked state.
   }

}