mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-12 22:14:07 -04:00
Implement "go to" block (#238)
* 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:
parent
886bcbe3c1
commit
5871672551
2 changed files with 33 additions and 0 deletions
src
|
@ -18,6 +18,7 @@ Scratch3MotionBlocks.prototype.getPrimitives = function() {
|
||||||
return {
|
return {
|
||||||
'motion_movesteps': this.moveSteps,
|
'motion_movesteps': this.moveSteps,
|
||||||
'motion_gotoxy': this.goToXY,
|
'motion_gotoxy': this.goToXY,
|
||||||
|
'motion_goto': this.goTo,
|
||||||
'motion_turnright': this.turnRight,
|
'motion_turnright': this.turnRight,
|
||||||
'motion_turnleft': this.turnLeft,
|
'motion_turnleft': this.turnLeft,
|
||||||
'motion_pointindirection': this.pointInDirection,
|
'motion_pointindirection': this.pointInDirection,
|
||||||
|
@ -48,6 +49,26 @@ Scratch3MotionBlocks.prototype.goToXY = function (args, util) {
|
||||||
util.target.setXY(x, y);
|
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) {
|
Scratch3MotionBlocks.prototype.turnRight = function (args, util) {
|
||||||
var degrees = Cast.toNumber(args.DEGREES);
|
var degrees = Cast.toNumber(args.DEGREES);
|
||||||
util.target.setDirection(util.target.direction + degrees);
|
util.target.setDirection(util.target.direction + degrees);
|
||||||
|
|
|
@ -69,6 +69,18 @@ function Runtime () {
|
||||||
this._cloneCounter = 0;
|
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.
|
* Event name for glowing a script.
|
||||||
* @const {string}
|
* @const {string}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue