mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
Enhance sprite delete behavior
This commit is contained in:
parent
40345384aa
commit
9c5d43e9d3
1 changed files with 4 additions and 2 deletions
|
@ -364,6 +364,8 @@ class VirtualMachine extends EventEmitter {
|
||||||
*/
|
*/
|
||||||
deleteSprite (targetId) {
|
deleteSprite (targetId) {
|
||||||
const target = this.runtime.getTargetById(targetId);
|
const target = this.runtime.getTargetById(targetId);
|
||||||
|
const targetIndexBeforeDelete = this.runtime.targets.map(t => t.id).indexOf(target.id);
|
||||||
|
|
||||||
if (target) {
|
if (target) {
|
||||||
if (!target.isSprite()) {
|
if (!target.isSprite()) {
|
||||||
throw new Error('Cannot delete non-sprite targets.');
|
throw new Error('Cannot delete non-sprite targets.');
|
||||||
|
@ -379,8 +381,8 @@ class VirtualMachine extends EventEmitter {
|
||||||
this.runtime.disposeTarget(sprite.clones[i]);
|
this.runtime.disposeTarget(sprite.clones[i]);
|
||||||
// Ensure editing target is switched if we are deleting it.
|
// Ensure editing target is switched if we are deleting it.
|
||||||
if (clone === currentEditingTarget) {
|
if (clone === currentEditingTarget) {
|
||||||
const lastTargetIndex = this.runtime.targets.length - 1;
|
const nextTargetIndex = Math.min(this.runtime.targets.length - 1, targetIndexBeforeDelete);
|
||||||
this.setEditingTarget(this.runtime.targets[lastTargetIndex].id);
|
this.setEditingTarget(this.runtime.targets[nextTargetIndex].id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Sprite object should be deleted by GC.
|
// Sprite object should be deleted by GC.
|
||||||
|
|
Loading…
Reference in a new issue