Implement "distance to" block (#239)

* Implement "distance-to" block

* distance-to in stage should always be 10000
This commit is contained in:
Liam 2016-10-04 22:26:59 -03:00 committed by Tim Mickel
parent 9b07889b3f
commit 30535d8e6e

View file

@ -16,6 +16,7 @@ Scratch3SensingBlocks.prototype.getPrimitives = function() {
return {
'sensing_touchingcolor': this.touchingColor,
'sensing_coloristouchingcolor': this.colorTouchingColor,
'sensing_distanceto': this.distanceTo,
'sensing_timer': this.getTimer,
'sensing_resettimer': this.resetTimer,
'sensing_mousex': this.getMouseX,
@ -37,6 +38,28 @@ Scratch3SensingBlocks.prototype.colorTouchingColor = function (args, util) {
return util.target.colorIsTouchingColor(targetColor, maskColor);
};
Scratch3SensingBlocks.prototype.distanceTo = function (args, util) {
if (util.target.isStage) return 10000;
var targetX = 0;
var targetY = 0;
if (args.DISTANCETOMENU === '_mouse_') {
targetX = util.ioQuery('mouse', 'getX');
targetY = util.ioQuery('mouse', 'getY');
} else {
var distTarget = this.runtime.getSpriteTargetByName(
args.DISTANCETOMENU
);
if (!distTarget) return 10000;
targetX = distTarget.x;
targetY = distTarget.y;
}
var dx = util.target.x - targetX;
var dy = util.target.y - targetY;
return Math.sqrt((dx * dx) + (dy * dy));
};
Scratch3SensingBlocks.prototype.getTimer = function (args, util) {
return util.ioQuery('clock', 'projectTimer');
};