Clones inherit music custom state

This commit is contained in:
Eric Rosenbaum 2018-04-05 11:27:54 -04:00
parent 21ae14c499
commit 88b13e218d

View file

@ -74,6 +74,9 @@ class Scratch3MusicBlocks {
this._bufferSources = []; this._bufferSources = [];
this._loadAllSounds(); this._loadAllSounds();
this._onTargetCreated = this._onTargetCreated.bind(this);
runtime.on('targetWasCreated', this._onTargetCreated);
} }
/** /**
@ -441,6 +444,22 @@ class Scratch3MusicBlocks {
return musicState; return musicState;
} }
/**
* When a music-playing Target is cloned, clone the music state.
* @param {Target} newTarget - the newly created target.
* @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
* @listens Runtime#event:targetWasCreated
* @private
*/
_onTargetCreated (newTarget, sourceTarget) {
if (sourceTarget) {
const musicState = sourceTarget.getCustomState(Scratch3MusicBlocks.STATE_KEY);
if (musicState) {
newTarget.setCustomState(Scratch3MusicBlocks.STATE_KEY, Clone.simple(musicState));
}
}
}
/** /**
* @returns {object} metadata for this extension and its blocks. * @returns {object} metadata for this extension and its blocks.
*/ */