diff --git a/src/blocks/scratch3_event.js b/src/blocks/scratch3_event.js index ba496a172..a486bda09 100644 --- a/src/blocks/scratch3_event.js +++ b/src/blocks/scratch3_event.js @@ -24,6 +24,7 @@ class Scratch3EventBlocks { */ getPrimitives () { return { + event_whentouchingobject: this.touchingObject, event_broadcast: this.broadcast, event_broadcastandwait: this.broadcastAndWait, event_whengreaterthan: this.hatGreaterThanPredicate @@ -41,6 +42,10 @@ class Scratch3EventBlocks { event_whenthisspriteclicked: { restartExistingThreads: true }, + event_whentouchingobject: { + restartExistingThreads: false, + edgeActivated: true + }, event_whenstageclicked: { restartExistingThreads: true }, @@ -57,6 +62,10 @@ class Scratch3EventBlocks { }; } + touchingObject (args, util) { + return util.target.isTouchingObject(args.TOUCHINGOBJECTMENU); + } + hatGreaterThanPredicate (args, util) { const option = Cast.toString(args.WHENGREATERTHANMENU).toLowerCase(); const value = Cast.toNumber(args.VALUE); diff --git a/src/blocks/scratch3_sensing.js b/src/blocks/scratch3_sensing.js index f140997cf..9e1d37a50 100644 --- a/src/blocks/scratch3_sensing.js +++ b/src/blocks/scratch3_sensing.js @@ -150,16 +150,7 @@ class Scratch3SensingBlocks { } touchingObject (args, util) { - const requestedObject = args.TOUCHINGOBJECTMENU; - if (requestedObject === '_mouse_') { - const mouseX = util.ioQuery('mouse', 'getClientX'); - const mouseY = util.ioQuery('mouse', 'getClientY'); - return util.target.isTouchingPoint(mouseX, mouseY); - } else if (requestedObject === '_edge_') { - return util.target.isTouchingEdge(); - } - return util.target.isTouchingSprite(requestedObject); - + return util.target.isTouchingObject(args.TOUCHINGOBJECTMENU); } touchingColor (args, util) { diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index d6fa70651..37c1b5615 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -765,6 +765,23 @@ class RenderedTarget extends Target { return null; } + /** + * Return whether this target is touching the mouse, an edge, or a sprite. + * @param {string} requestedObject an id for mouse or edge, or a sprite name. + * @return {boolean} True if the sprite is touching the object. + */ + isTouchingObject (requestedObject) { + if (requestedObject === '_mouse_') { + if (!this.runtime.ioDevices.mouse) return false; + const mouseX = this.runtime.ioDevices.mouse.getClientX(); + const mouseY = this.runtime.ioDevices.mouse.getClientY(); + return this.isTouchingPoint(mouseX, mouseY); + } else if (requestedObject === '_edge_') { + return this.isTouchingEdge(); + } + return this.isTouchingSprite(requestedObject); + } + /** * Return whether touching a point. * @param {number} x X coordinate of test point.