diff --git a/src/blocks/scratch3_control.js b/src/blocks/scratch3_control.js index 15a43fb64..4d23bed93 100644 --- a/src/blocks/scratch3_control.js +++ b/src/blocks/scratch3_control.js @@ -7,6 +7,12 @@ class Scratch3ControlBlocks { * @type {Runtime} */ this.runtime = runtime; + + /** + * The "counter" block value. For compatibility with 2.0. + * @type {number} + */ + this._counter = 0; } /** @@ -25,7 +31,10 @@ class Scratch3ControlBlocks { control_if_else: this.ifElse, control_stop: this.stop, control_create_clone_of: this.createClone, - control_delete_this_clone: this.deleteClone + control_delete_this_clone: this.deleteClone, + control_get_counter: this.getCounter, + control_incr_counter: this.incrCounter, + control_clear_counter: this.clearCounter }; } @@ -146,6 +155,18 @@ class Scratch3ControlBlocks { this.runtime.disposeTarget(util.target); this.runtime.stopForTarget(util.target); } + + getCounter () { + return this._counter; + } + + clearCounter () { + this._counter = 0; + } + + incrCounter () { + this._counter++; + } } module.exports = Scratch3ControlBlocks; diff --git a/src/serialization/sb2_specmap.js b/src/serialization/sb2_specmap.js index 637ccbab9..eb6e91247 100644 --- a/src/serialization/sb2_specmap.js +++ b/src/serialization/sb2_specmap.js @@ -815,6 +815,21 @@ const specMap = { argMap: [ ] }, + 'COUNT': { + opcode: 'control_get_counter', + argMap: [ + ] + }, + 'INCR_COUNT': { + opcode: 'control_incr_counter', + argMap: [ + ] + }, + 'CLR_COUNT': { + opcode: 'control_clear_counter', + argMap: [ + ] + }, 'touching:': { opcode: 'sensing_touchingobject', argMap: [ diff --git a/test/unit/blocks_control.js b/test/unit/blocks_control.js index 3e89ab958..6bf5cb438 100644 --- a/test/unit/blocks_control.js +++ b/test/unit/blocks_control.js @@ -169,3 +169,20 @@ test('stop', t => { t.strictEqual(state.stopThisScript, 1); t.end(); }); + +test('counter, incrCounter, clearCounter', t => { + const rt = new Runtime(); + const c = new Control(rt); + + // Default value + t.strictEqual(c.getCounter(), 0); + + c.incrCounter(); + c.incrCounter(); + t.strictEqual(c.getCounter(), 2); + + c.clearCounter(); + t.strictEqual(c.getCounter(), 0); + + t.end(); +});