Merge pull request #594 from paulkaplan/select-other-sprites-after-delete

Select the last target instead of the first after deleting
This commit is contained in:
Paul Kaplan 2017-06-12 08:41:20 -04:00 committed by GitHub
commit f968edec54

View file

@ -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,7 +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) {
this.setEditingTarget(this.runtime.targets[0].id); const nextTargetIndex = Math.min(this.runtime.targets.length - 1, targetIndexBeforeDelete);
this.setEditingTarget(this.runtime.targets[nextTargetIndex].id);
} }
} }
// Sprite object should be deleted by GC. // Sprite object should be deleted by GC.