node_modules/scratch-vm/src/virtual-machine.js
/**
     * Delete a sprite and all its clones.
     * @param {string} targetId ID of a target whose sprite to delete.
     */
Delete a sprite and all its clones.
@param {string} targetId ID of a target whose sprite to delete.
Delete a sprite and all its clones . @param { string } targetId ID of a target whose sprite to delete .
deleteSprite (targetId) {
        const target = this.runtime.getTargetById(targetId);
        if (target) {
            const targetIndexBeforeDelete = this.runtime.targets.map(t => t.id).indexOf(target.id);
            if (!target.isSprite()) {
                throw new Error('Cannot delete non-sprite targets.');
            }
            const sprite = target.sprite;
            if (!sprite) {
                throw new Error('No sprite associated with this target.');
            }
            this.runtime.requestRemoveMonitorByTargetId(targetId);
            const currentEditingTarget = this.editingTarget;
            for (let i = 0; i < sprite.clones.length; i++) {
                const clone = sprite.clones[i];
                this.runtime.stopForTarget(sprite.clones[i]);
                this.runtime.disposeTarget(sprite.clones[i]);
                if (clone === currentEditingTarget) {
                    const nextTargetIndex = Math.min(this.runtime.targets.length - 1, targetIndexBeforeDelete);
                    if (this.runtime.targets.length > 0){
                        this.setEditingTarget(this.runtime.targets[nextTargetIndex].id);
                    } else {
                        this.editingTarget = null;
                    }
                }
            }
            this.emitTargetsUpdate();
        } else {
            throw new Error('No target with the provided id.');
        }
    }
deleteSprite ( targetId ) { const target = this . runtime . getTargetById ( targetId ) ; if ( target ) { const targetIndexBeforeDelete = this . runtime . targets . map ( t => t . id ) . indexOf ( target . id ) ; if ( ! target . isSprite ( ) ) { throw new Error ( 'Cannot delete non-sprite targets.' ) ; } const sprite = target . sprite ; if ( ! sprite ) { throw new Error ( 'No sprite associated with this target.' ) ; } this . runtime . requestRemoveMonitorByTargetId ( targetId ) ; const currentEditingTarget = this . editingTarget ; for ( let i = 0 ; i < sprite . clones . length ; i ++ ) { const clone = sprite . clones [ i ] ; this . runtime . stopForTarget ( sprite . clones [ i ] ) ; this . runtime . disposeTarget ( sprite . clones [ i ] ) ; if ( clone === currentEditingTarget ) { const nextTargetIndex = Math . min ( this . runtime . targets . length - 1 , targetIndexBeforeDelete ) ; if ( this . runtime . targets . length > 0 ) { this . setEditingTarget ( this . runtime . targets [ nextTargetIndex ] . id ) ; } else { this . editingTarget = null ; } } } this . emitTargetsUpdate ( ) ; } else { throw new Error ( 'No target with the provided id.' ) ; } }
Delete a sprite and all its clones.
Delete a sprite and all its clones .
// Ensure editing target is switched if we are deleting it. // Sprite object should be deleted by GC.