Skip to content
Snippets Groups Projects
Commit 7a8fd5ab authored by Karishma Chadha's avatar Karishma Chadha
Browse files

randomize sprite positions when added from image file or from library (through...

randomize sprite positions when added from image file or from library (through 'choose from library' and 'surprise')
parent 32236805
Branches
Tags
No related merge requests found
......@@ -6,6 +6,7 @@ import VM from 'scratch-vm';
import analytics from '../lib/analytics';
import spriteLibraryContent from '../lib/libraries/sprites.json';
import randomizeSpritePosition from '../lib/randomize-sprite-position';
import spriteTags from '../lib/libraries/sprite-tags';
import LibraryComponent from '../components/library/library.jsx';
......@@ -39,6 +40,8 @@ class SpriteLibrary extends React.PureComponent {
clearInterval(this.intervalId);
}
handleItemSelect (item) {
// Randomize position of library sprite
randomizeSpritePosition(item);
this.props.vm.addSprite(JSON.stringify(item.json)).then(() => {
this.props.onActivateBlocksTab();
});
......
......@@ -20,6 +20,7 @@ import sharedMessages from '../lib/shared-messages';
import {emptySprite} from '../lib/empty-assets';
import {highlightTarget} from '../reducers/targets';
import {fetchSprite, fetchCode} from '../lib/backpack-api';
import randomizeSpritePosition from '../lib/randomize-sprite-position';
class TargetPane extends React.Component {
constructor (props) {
......@@ -117,6 +118,7 @@ class TargetPane extends React.Component {
}
handleSurpriseSpriteClick () {
const item = spriteLibraryContent[Math.floor(Math.random() * spriteLibraryContent.length)];
randomizeSpritePosition(item);
this.props.vm.addSprite(JSON.stringify(item.json))
.then(this.handleActivateBlocksTab);
}
......
import {BitmapAdapter} from 'scratch-svg-renderer';
import log from './log.js';
import randomizeSpritePosition from './randomize-sprite-position.js';
/**
* Extract the file name given a string of the form fileName + ext
......@@ -199,7 +200,7 @@ const spriteUpload = function (fileData, fileType, spriteName, storage, handleSp
const newSprite = {
name: spriteName,
isStage: false,
x: 0,
x: 0, // x/y will be randomized below
y: 0,
visible: true,
size: 100,
......@@ -212,6 +213,7 @@ const spriteUpload = function (fileData, fileType, spriteName, storage, handleSp
costumes: [vmCostume],
sounds: [] // TODO are all of these necessary?
};
randomizeSpritePosition(newSprite);
// TODO probably just want sprite upload to handle this object directly
handleSprite(JSON.stringify(newSprite));
}));
......
const randomizeSpritePosition = spriteObject => {
// https://github.com/LLK/scratch-flash/blob/689f3c79a7e8b2e98f5be80056d877f303a8d8ba/src/Scratch.as#L1385
const randomX = Math.floor((200 * Math.random()) - 100);
const randomY = Math.floor((100 * Math.random()) - 50);
if (spriteObject.hasOwnProperty('json')) {
// Library sprite object
spriteObject.json.scratchX = randomX;
spriteObject.json.scratchY = randomY;
} else if (spriteObject.hasOwnProperty('x') && spriteObject.hasOwnProperty('y')) {
// Scratch 3 sprite object
spriteObject.x = randomX;
spriteObject.y = randomY;
}
};
export default randomizeSpritePosition;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment