Implement "go to" block ()

* Implement "go to" block

* Add a missing semicolon

My text editor doesn't automatically insert them and I'm not used to using
semicolons so much. :(

* Implement go-to-random

* Clean up the go-to-random code a bit

* Add rounding to _random_ picks
This commit is contained in:
Liam 2016-10-04 19:19:52 -03:00 committed by Tim Mickel
parent 886bcbe3c1
commit 5871672551
2 changed files with 33 additions and 0 deletions

View file

@ -18,6 +18,7 @@ Scratch3MotionBlocks.prototype.getPrimitives = function() {
return {
'motion_movesteps': this.moveSteps,
'motion_gotoxy': this.goToXY,
'motion_goto': this.goTo,
'motion_turnright': this.turnRight,
'motion_turnleft': this.turnLeft,
'motion_pointindirection': this.pointInDirection,
@ -48,6 +49,26 @@ Scratch3MotionBlocks.prototype.goToXY = function (args, util) {
util.target.setXY(x, y);
};
Scratch3MotionBlocks.prototype.goTo = function (args, util) {
var targetX = 0;
var targetY = 0;
if (args.TO === '_mouse_') {
targetX = util.ioQuery('mouse', 'getX');
targetY = util.ioQuery('mouse', 'getY');
} else if (args.TO === '_random_') {
var stageWidth = this.runtime.constructor.STAGE_WIDTH;
var stageHeight = this.runtime.constructor.STAGE_HEIGHT;
targetX = Math.round(stageWidth * (Math.random() - 0.5));
targetY = Math.round(stageHeight * (Math.random() - 0.5));
} else {
var goToTarget = this.runtime.getSpriteTargetByName(args.TO);
if (!goToTarget) return;
targetX = goToTarget.x;
targetY = goToTarget.y;
}
util.target.setXY(targetX, targetY);
};
Scratch3MotionBlocks.prototype.turnRight = function (args, util) {
var degrees = Cast.toNumber(args.DEGREES);
util.target.setDirection(util.target.direction + degrees);

View file

@ -69,6 +69,18 @@ function Runtime () {
this._cloneCounter = 0;
}
/**
* Width of the stage, in pixels.
* @const {number}
*/
Runtime.STAGE_WIDTH = 480;
/**
* Height of the stage, in pixels.
* @const {number}
*/
Runtime.STAGE_HEIGHT = 360;
/**
* Event name for glowing a script.
* @const {string}