Iphone 由于跟踪,Sprite具有抖动效果
我有一个精灵跟踪屏幕上的物体,并朝着它们移动 该方法按计划运行,基本上如下所示:Iphone 由于跟踪,Sprite具有抖动效果,iphone,ios,ipad,cocos2d-iphone,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;
- (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)时,鱼会快速抖动并停止
我怎样才能消除这种跳汰效应?一旦鱼到达气泡的位置,我想阻止它移动。或者任何能使它平滑的替代方案。感谢您的帮助。Zeno's arrow。每走一步,把鱼的速度减半。当你到达你的距离时,你能把鱼对准另一个泡泡吗?否则,当鱼在你的距离内时,你可以将其“停放”一段随机时间,并在到期时重新追逐。。。。。这取决于你“接触”泡泡时想要的行为。停鱼可以奏效。最好的方法是什么?我想我可以用这样的东西!但是,你认为我怎样才能增加延迟呢?一旦鱼停下来,我必须在3秒钟后告诉它。我不能真正使用带延迟的选择器,因为它会在整个方法重复时运行多次。有什么想法吗?存储驻车时间,并在存储+延迟时取消连接。
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.
}
}