diff --git a/src/extensions/scratch3_music/index.js b/src/extensions/scratch3_music/index.js index 99912a94c..3a5552c45 100644 --- a/src/extensions/scratch3_music/index.js +++ b/src/extensions/scratch3_music/index.js @@ -74,6 +74,9 @@ class Scratch3MusicBlocks { this._bufferSources = []; this._loadAllSounds(); + + this._onTargetCreated = this._onTargetCreated.bind(this); + runtime.on('targetWasCreated', this._onTargetCreated); } /** @@ -441,6 +444,22 @@ class Scratch3MusicBlocks { 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. */