Skip to content

Commit

Permalink
fix(sprites): fix empty_sprite command not deleting all sprites (#118)
Browse files Browse the repository at this point in the history
Caused by a typical "deleting while iterating" array bug
  • Loading branch information
liana-p committed Jul 6, 2023
1 parent b16790e commit 99bae2d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/narrat/src/stores/screen-objects-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ export const useScreenObjects = defineStore('screenObjects', {
if (typeof object === 'string') {
object = this.getObject(object);
}
for (const child of object.children) {
this.destroyObject(child);
for (let i = object.children.length - 1; i >= 0; i--) {
this.destroyObject(object.children[i]);
}
if (object.parent) {
const parent = object.parent;
Expand Down Expand Up @@ -259,14 +259,18 @@ export const useScreenObjects = defineStore('screenObjects', {
return objects as any as { [key: string]: ScreenObjectState };
},
emptyLayer(layer: number) {
for (const obj of this.tree) {
// Reverse looping since we may be deleting elements
for (let i = this.tree.length - 1; i >= 0; i--) {
const obj = this.tree[i];
if (obj.layer === layer) {
this.destroyObject(obj);
}
}
},
emptyAllLayers() {
for (const obj of this.tree) {
// Reverse looping since we may be deleting elements
for (let i = this.tree.length - 1; i >= 0; i--) {
const obj = this.tree[i];
this.destroyObject(obj);
}
},
Expand Down

0 comments on commit 99bae2d

Please sign in to comment.