Skip to content

Commit

Permalink
Merge pull request #1088 from paulkaplan/fix-say-timing
Browse files Browse the repository at this point in the history
Track say/thinks per target so they can maintain correct timing.
  • Loading branch information
paulkaplan committed Apr 27, 2018
2 parents 1ed5d57 + c2bb1e3 commit ad4f565
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/blocks/scratch3_looks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Cast = require('../util/cast');
const Clone = require('../util/clone');
const RenderedTarget = require('../sprites/rendered-target');
const uid = require('../util/uid');

/**
* @typedef {object} BubbleState - the bubble state associated with a particular target.
Expand All @@ -10,6 +11,8 @@ const RenderedTarget = require('../sprites/rendered-target');
* See _renderBubble for explanation of this optimization.
* @property {string} text - the text of the bubble.
* @property {string} type - the type of the bubble, "say" or "think"
* @property {?string} usageId - ID indicating the most recent usage of the say/think bubble.
* Used for comparison when determining whether to clear a say/think bubble.
*/

class Scratch3LooksBlocks {
Expand Down Expand Up @@ -44,7 +47,8 @@ class Scratch3LooksBlocks {
onSpriteRight: true,
skinId: null,
text: '',
type: 'say'
type: 'say',
usageId: null
};
}

Expand Down Expand Up @@ -224,6 +228,7 @@ class Scratch3LooksBlocks {
const bubbleState = this._getBubbleState(target);
bubbleState.type = type;
bubbleState.text = text;
bubbleState.usageId = uid();
this._renderBubble(target);
}

Expand Down Expand Up @@ -277,12 +282,15 @@ class Scratch3LooksBlocks {

sayforsecs (args, util) {
this.say(args, util);
const _target = util.target;
const target = util.target;
const usageId = this._getBubbleState(target).usageId;
return new Promise(resolve => {
this._bubbleTimeout = setTimeout(() => {
this._bubbleTimeout = null;
// Clear say bubble and proceed.
this._updateBubble(_target, 'say', '');
// Clear say bubble if it hasn't been changed and proceed.
if (this._getBubbleState(target).usageId === usageId) {
this._updateBubble(target, 'say', '');
}
resolve();
}, 1000 * args.SECS);
});
Expand All @@ -294,12 +302,15 @@ class Scratch3LooksBlocks {

thinkforsecs (args, util) {
this.think(args, util);
const _target = util.target;
const target = util.target;
const usageId = this._getBubbleState(target).usageId;
return new Promise(resolve => {
this._bubbleTimeout = setTimeout(() => {
this._bubbleTimeout = null;
// Clear say bubble and proceed.
this._updateBubble(_target, 'think', '');
// Clear think bubble if it hasn't been changed and proceed.
if (this._getBubbleState(target).usageId === usageId) {
this._updateBubble(target, 'think', '');
}
resolve();
}, 1000 * args.SECS);
});
Expand Down

0 comments on commit ad4f565

Please sign in to comment.