Implement the point-towards block (#235)

* Implement the point-towards block

* Pointing towards a nonexistent sprite should do nothing, not throw an error
This commit is contained in:
Liam 2016-10-04 16:20:53 -03:00 committed by Tim Mickel
parent 1f30b175a4
commit 886bcbe3c1

View file

@ -21,6 +21,7 @@ Scratch3MotionBlocks.prototype.getPrimitives = function() {
'motion_turnright': this.turnRight, 'motion_turnright': this.turnRight,
'motion_turnleft': this.turnLeft, 'motion_turnleft': this.turnLeft,
'motion_pointindirection': this.pointInDirection, 'motion_pointindirection': this.pointInDirection,
'motion_pointtowards': this.pointTowards,
'motion_glidesecstoxy': this.glide, 'motion_glidesecstoxy': this.glide,
'motion_setrotationstyle': this.setRotationStyle, 'motion_setrotationstyle': this.setRotationStyle,
'motion_changexby': this.changeX, 'motion_changexby': this.changeX,
@ -62,6 +63,25 @@ Scratch3MotionBlocks.prototype.pointInDirection = function (args, util) {
util.target.setDirection(direction); util.target.setDirection(direction);
}; };
Scratch3MotionBlocks.prototype.pointTowards = function (args, util) {
var targetX = 0;
var targetY = 0;
if (args.TOWARDS === '_mouse_') {
targetX = util.ioQuery('mouse', 'getX');
targetY = util.ioQuery('mouse', 'getY');
} else {
var pointTarget = this.runtime.getSpriteTargetByName(args.TOWARDS);
if (!pointTarget) return;
targetX = pointTarget.x;
targetY = pointTarget.y;
}
var dx = targetX - util.target.x;
var dy = targetY - util.target.y;
var direction = 90 - MathUtil.radToDeg(Math.atan2(dy, dx));
util.target.setDirection(direction);
};
Scratch3MotionBlocks.prototype.glide = function (args, util) { Scratch3MotionBlocks.prototype.glide = function (args, util) {
if (!util.stackFrame.timer) { if (!util.stackFrame.timer) {
// First time: save data for future use. // First time: save data for future use.