Merge pull request #1160 from Kenny2github/patch-1

Round (x position) and (y position) to 8 places
This commit is contained in:
Ray Schamp 2018-05-24 13:58:16 -04:00 committed by GitHub
commit 3b49f45b9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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.
* @param {!number} x New X coordinate, in Scratch coordinates.
@ -250,6 +261,8 @@ class RenderedTarget extends Target {
const oldY = this.y;
if (this.renderer) {
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.y = position[1];
@ -261,8 +274,8 @@ class RenderedTarget extends Target {
this.runtime.requestRedraw();
}
} else {
this.x = x;
this.y = y;
this.x = this._roundCoord(x, 8);
this.y = this._roundCoord(y, 8);
}
this.emit(RenderedTarget.EVENT_TARGET_MOVED, this, oldX, oldY, force);
this.runtime.requestTargetsUpdate(this);