mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
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:
parent
1f30b175a4
commit
886bcbe3c1
1 changed files with 20 additions and 0 deletions
|
@ -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.
|
||||||
|
|
Loading…
Reference in a new issue