Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/sprites/rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,17 @@ class RenderedTarget extends Target {
/**
* Delete a sound by index.
* @param {number} index Sound index to be deleted
* @return {object} The deleted sound object, or null if no sound was deleted.
*/
deleteSound (index) {
this.sprite.sounds = this.sprite.sounds
.slice(0, index)
.concat(this.sprite.sounds.slice(index + 1));
// Make sure the sound index is not out of bounds
if (index < 0 || index >= this.sprite.sounds.length) {
return null;
}
// Delete the sound at the given index
const deletedSound = this.sprite.sounds.splice(index, 1)[0];

This comment was marked as abuse.

This comment was marked as abuse.

this.runtime.requestTargetsUpdate(this);
return deletedSound;
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/virtual-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,20 @@ class VirtualMachine extends EventEmitter {
/**
* Delete a sound from the current editing target.
* @param {int} soundIndex - the index of the sound to be removed.
* @return {?Function} A function to restore the sound that was deleted,

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

* or null, if no sound was deleted.
*/
deleteSound (soundIndex) {
this.editingTarget.deleteSound(soundIndex);
const target = this.editingTarget;
const deletedSound = this.editingTarget.deleteSound(soundIndex);
if (deletedSound) {
const restoreFun = () => {
target.addSound(deletedSound);
this.emitTargetsUpdate();
};
return restoreFun;
}
return null;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/unit/sprites_rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ test('deleteSound', t => {
const renderer = new FakeRenderer();
a.renderer = renderer;

a.deleteSound(0);
const firstDeleted = a.deleteSound(0);
t.deepEqual(a.sprite.sounds, [o2, o3]);
t.deepEqual(firstDeleted, o1);

// Allows deleting the only sound
a.sprite.sounds = [o1];
Expand Down
32 changes: 29 additions & 3 deletions test/unit/virtual-machine.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
const test = require('tap').test;
const VirtualMachine = require('../../src/virtual-machine.js');
const Sprite = require('../../src/sprites/sprite.js');
const Variable = require('../../src/engine/variable.js');
const VirtualMachine = require('../../src/virtual-machine');
const Sprite = require('../../src/sprites/sprite');
const Variable = require('../../src/engine/variable');
const adapter = require('../../src/engine/adapter');
const events = require('../fixtures/events.json');
const Runtime = require('../../src/engine/runtime');
const RenderedTarget = require('../../src/sprites/rendered-target');

test('deleteSound returns function after deleting or null if nothing was deleted', t => {
const vm = new VirtualMachine();
const sprite = new Sprite();
sprite.sounds = [{id: 1}, {id: 2}, {id: 3}];
const rt = new Runtime();
const target = new RenderedTarget(sprite, rt);
vm.editingTarget = target;

const addFun = vm.deleteSound(1);
t.equal(sprite.sounds.length, 2);
t.equal(sprite.sounds[0].id, 1);
t.equal(sprite.sounds[1].id, 3);
t.type(addFun, 'function');

const noAddFun = vm.deleteSound(2);
t.equal(sprite.sounds.length, 2);
t.equal(sprite.sounds[0].id, 1);
t.equal(sprite.sounds[1].id, 3);
t.equal(noAddFun, null);

t.end();
});


test('addSprite throws on invalid string', t => {
const vm = new VirtualMachine();
Expand Down