Round (x position) and (y position) to 8 places

This commit is contained in:
Ken 2018-05-22 11:28:06 +08:00 committed by Kenny2github
parent 8c0f5a71f0
commit 5af6887cfe

View file

@ -237,6 +237,17 @@ class RenderedTarget extends Target {
}; };
} }
/**
* Round a number to n digits
* @param {number} value The number to be rounded
* @param {number} places The number of decimal places to round to
* @return {number} The rounded number
*/
_roundCoord (value, places) {
const power = Math.pow(10, places);
return Math.round(value * power) / power;
}
/** /**
* Set the X and Y coordinates. * Set the X and Y coordinates.
* @param {!number} x New X coordinate, in Scratch coordinates. * @param {!number} x New X coordinate, in Scratch coordinates.
@ -250,6 +261,8 @@ class RenderedTarget extends Target {
const oldY = this.y; const oldY = this.y;
if (this.renderer) { if (this.renderer) {
const position = this.renderer.getFencedPositionOfDrawable(this.drawableID, [x, y]); const position = this.renderer.getFencedPositionOfDrawable(this.drawableID, [x, y]);
position[0] = this._roundCoord(position[0], 8);
position[1] = this._roundCoord(position[1], 8);
this.x = position[0]; this.x = position[0];
this.y = position[1]; this.y = position[1];
@ -261,8 +274,8 @@ class RenderedTarget extends Target {
this.runtime.requestRedraw(); this.runtime.requestRedraw();
} }
} else { } else {
this.x = x; this.x = this._roundCoord(x, 8);
this.y = y; this.y = this._roundCoord(y, 8);
} }
this.emit(RenderedTarget.EVENT_TARGET_MOVED, this, oldX, oldY, force); this.emit(RenderedTarget.EVENT_TARGET_MOVED, this, oldX, oldY, force);
this.runtime.requestTargetsUpdate(this); this.runtime.requestTargetsUpdate(this);