Discuss Scratch

ScratchUser139
Scratcher
100+ posts

More Motion

I made this:
(function(Scratch) {
  'use strict';
  if (!Scratch.extensions.unsandboxed) {
    throw new Error('Motion Extension must run unsandboxed');
  }
  class MotionExtension {
    getInfo() {
      return {
        id: 'motionextension',
        name: 'Custom Motion',
        color1: '#0000FF',
        color2: '#000099',
        color3: '#000066',
        blocks: [
          {
            opcode: 'moveUpDown',
            blockType: Scratch.BlockType.COMMAND,
            text: 'move [DIRECTION] by [STEPS] steps',
            arguments: {
              DIRECTION: {
                type: Scratch.ArgumentType.STRING,
                menu: 'DIRECTION_MENU',
                defaultValue: 'up'
              },
              STEPS: {
                type: Scratch.ArgumentType.NUMBER,
                defaultValue: 10
              }
            }
          },
          {
            opcode: 'changeXY',
            blockType: Scratch.BlockType.COMMAND,
            text: 'change by x: [X] y: [Y]',
            arguments: {
              X: {
                type: Scratch.ArgumentType.NUMBER,
                defaultValue: 10
              },
              Y: {
                type: Scratch.ArgumentType.NUMBER,
                defaultValue: 10
              }
            }
          },
          {
            opcode: 'pointTowardsXY',
            blockType: Scratch.BlockType.COMMAND,
            text: 'point towards x: [X] y: [Y]',
            arguments: {
              X: {
                type: Scratch.ArgumentType.NUMBER,
                defaultValue: 0
              },
              Y: {
                type: Scratch.ArgumentType.NUMBER,
                defaultValue: 0
              }
            }
          },
          {
            opcode: 'turnAround',
            blockType: Scratch.BlockType.COMMAND,
            text: 'turn around'
          },
          {
            opcode: 'ifTouchingBounce',
            blockType: Scratch.BlockType.COMMAND,
            text: 'if touching [TARGET] then bounce',
            arguments: {
              TARGET: {
                type: Scratch.ArgumentType.STRING,
                menu: 'TOUCHING_MENU',
                defaultValue: 'mouse-pointer'
              }
            }
          },
          {
            opcode: 'moveToStage',
            blockType: Scratch.BlockType.COMMAND,
            text: 'move to stage [POSITION]',
            arguments: {
              POSITION: {
                type: Scratch.ArgumentType.STRING,
                menu: 'STAGE_POSITION_MENU',
                defaultValue: 'bottom-left'
              }
            }
          }
        ],
        menus: {
          DIRECTION_MENU: {
            acceptReporters: false,
            items: ['up', 'down']
          },
          TOUCHING_MENU: {
            acceptReporters: false,
            items: ['mouse-pointer', 'edge', 'random direction'].concat(Scratch.vm.runtime.targets
              .filter(target => target.isSprite && !target.isStage)
              .map(target => target.getName()))
          },
          STAGE_POSITION_MENU: {
            acceptReporters: false,
            items: [
              'bottom-left',
              'bottom',
              'bottom-right',
              'middle',
              'top-left',
              'top',
              'top-right',
              'left',
              'right'
            ]
          }
        }
      };
    }
    moveUpDown(args, util) {
      const steps = Scratch.Cast.toNumber(args.STEPS);
      const direction = args.DIRECTION === 'up' ? 90 : -90;
      const radians = Scratch.MathUtil.degToRad(direction);
      const dx = Math.cos(radians) * steps;
      const dy = Math.sin(radians) * steps;
      util.target.setXY(util.target.x + dx, util.target.y + dy);
    }
    changeXY(args, util) {
      const x = Scratch.Cast.toNumber(args.X);
      const y = Scratch.Cast.toNumber(args.Y);
      util.target.setXY(util.target.x + x, util.target.y + y);
    }
    pointTowardsXY(args, util) {
      const x = Scratch.Cast.toNumber(args.X);
      const y = Scratch.Cast.toNumber(args.Y);
      const dx = x - util.target.x;
      const dy = y - util.target.y;
      const direction = Scratch.MathUtil.radToDeg(Math.atan2(dy, dx));
      util.target.setDirection(direction);
    }
    turnAround(args, util) {
      util.target.setDirection(util.target.direction + 180);
    }
    ifTouchingBounce(args, util) {
      const target = args.TARGET;
      let isTouching = false;
      if (target === 'mouse-pointer') {
        isTouching = util.target.isTouchingMouse();
      } else if (target === 'edge') {
        isTouching = util.target.isTouchingEdge();
      } else if (target === 'random direction') {
        util.target.setDirection(Math.random() * 360);
        return;
      } else {
        isTouching = util.target.isTouchingSprite(target);
      }
      if (isTouching) {
        util.target.setDirection(-util.target.direction);
      }
    }
    moveToStage(args, util) {
      const stageWidth = 480;
      const stageHeight = 360;
      let x = 0;
      let y = 0;
      switch (args.POSITION) {
        case 'bottom-left':
          x = -stageWidth / 2;
          y = -stageHeight / 2;
          break;
        case 'bottom':
          x = 0;
          y = -stageHeight / 2;
          break;
        case 'bottom-right':
          x = stageWidth / 2;
          y = -stageHeight / 2;
          break;
        case 'middle':
          x = 0;
          y = 0;
          break;
        case 'top-left':
          x = -stageWidth / 2;
          y = stageHeight / 2;
          break;
        case 'top':
          x = 0;
          y = stageHeight / 2;
          break;
        case 'top-right':
          x = stageWidth / 2;
          y = stageHeight / 2;
          break;
        case 'left':
          x = -stageWidth / 2;
          y = 0;
          break;
        case 'right':
          x = stageWidth / 2;
          y = 0;
          break;
      }
      util.target.setXY(x, y);
    }
  }
  Scratch.extensions.register(new MotionExtension());
})(Scratch);
Nino3245
Scratcher
51 posts

More Motion

edit: please ignore, this is a place for sharing extensions
this isn't the place for sharing extensions you made

Last edited by Nino3245 (Yesterday 20:44:36)

Frosty11y669
Scratcher
4 posts

More Motion

Nino3245 wrote:

edit: please ignore, this is a place for sharing extensions
this isn't the place for sharing extensions you made
We can't even use them without mods
Edit: never mind

Last edited by Frosty11y669 (Yesterday 20:45:52)

Powered by DjangoBB