1
我們從服務器發送到客戶端的球COORDS每300毫秒。我們必須插入座標來使移動平穩。下面是代碼(AS3):COORDS插值
private function run(event:Event):void
{
// Current frame ball position
var currentPosition:Point = new Point(this.x, this.y);
// Vector of the speed
_velocity = _destinationPoint.subtract(currentPosition);
// Interpolation
// Game.timeLapse - time from last package with coordinates (last change of destinationPoint)
// stage.frameRate - fps
_velocity.normalize(_velocity.length * 1000/Game.timeLapse/stage.frameRate);
// If ball isn't at the end of the path, move it
if (Point.distance(currentPosition, _destinationPoint) > 1) {
this.x += _velocity.x;
this.y += _velocity.y;
} else {
// Otherwise (we are at the end of the path - remove listener from this event
this.removeEventListener(Event.ENTER_FRAME, run);
this.dispatchEvent(new GameEvent(GameEvent.PLAYER_STOP));
}
}
問題是在下面的圖片描述:
紅點 - 目標點
黑線 - 線從curret點到目的地而不 正常化
綠色點綴 - 球的路徑
也許有一種方法,使移動順暢,但更準確?