mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
Make renderer/self nullable in the Clone
Fixes a test issue where renderer is not necessarily defined.
This commit is contained in:
parent
67c3ceff86
commit
2c031d87f6
1 changed files with 51 additions and 22 deletions
|
@ -9,20 +9,37 @@ var Target = require('../engine/target');
|
||||||
*/
|
*/
|
||||||
function Clone(spriteBlocks) {
|
function Clone(spriteBlocks) {
|
||||||
Target.call(this, spriteBlocks);
|
Target.call(this, spriteBlocks);
|
||||||
|
/**
|
||||||
|
* Reference to the global renderer for this VM, if one exists.
|
||||||
|
* @type {?RenderWebGLWorker}
|
||||||
|
*/
|
||||||
|
this.renderer = null;
|
||||||
|
// If this is not true, there is no renderer (e.g., running in a test env).
|
||||||
|
if (typeof self !== 'undefined' && self.renderer) {
|
||||||
|
// Pull from `self.renderer`.
|
||||||
|
this.renderer = self.renderer;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* ID of the drawable for this clone returned by the renderer, if rendered.
|
||||||
|
* @type {?Number}
|
||||||
|
*/
|
||||||
this.drawableID = null;
|
this.drawableID = null;
|
||||||
|
|
||||||
this.initDrawable();
|
this.initDrawable();
|
||||||
}
|
}
|
||||||
util.inherits(Clone, Target);
|
util.inherits(Clone, Target);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a clone's drawable with the renderer.
|
* Create a clone's drawable with the this.renderer.
|
||||||
*/
|
*/
|
||||||
Clone.prototype.initDrawable = function () {
|
Clone.prototype.initDrawable = function () {
|
||||||
var createPromise = self.renderer.createDrawable();
|
if (this.renderer) {
|
||||||
var instance = this;
|
var createPromise = this.renderer.createDrawable();
|
||||||
createPromise.then(function (id) {
|
var instance = this;
|
||||||
instance.drawableID = id;
|
createPromise.then(function (id) {
|
||||||
});
|
instance.drawableID = id;
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Clone-level properties.
|
// Clone-level properties.
|
||||||
|
@ -79,9 +96,11 @@ Clone.prototype.effects = {
|
||||||
Clone.prototype.setXY = function (x, y) {
|
Clone.prototype.setXY = function (x, y) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
self.renderer.updateDrawableProperties(this.drawableID, {
|
if (this.renderer) {
|
||||||
position: [this.x, this.y]
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
||||||
});
|
position: [this.x, this.y]
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -91,9 +110,11 @@ Clone.prototype.setXY = function (x, y) {
|
||||||
Clone.prototype.setDirection = function (direction) {
|
Clone.prototype.setDirection = function (direction) {
|
||||||
// Keep direction between -179 and +180.
|
// Keep direction between -179 and +180.
|
||||||
this.direction = MathUtil.wrapClamp(direction, -179, 180);
|
this.direction = MathUtil.wrapClamp(direction, -179, 180);
|
||||||
self.renderer.updateDrawableProperties(this.drawableID, {
|
if (this.renderer) {
|
||||||
direction: this.direction
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
||||||
});
|
direction: this.direction
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -116,9 +137,11 @@ Clone.prototype.setSay = function (type, message) {
|
||||||
*/
|
*/
|
||||||
Clone.prototype.setVisible = function (visible) {
|
Clone.prototype.setVisible = function (visible) {
|
||||||
this.visible = visible;
|
this.visible = visible;
|
||||||
self.renderer.updateDrawableProperties(this.drawableID, {
|
if (this.renderer) {
|
||||||
visible: this.visible
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
||||||
});
|
visible: this.visible
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -128,9 +151,11 @@ Clone.prototype.setVisible = function (visible) {
|
||||||
Clone.prototype.setSize = function (size) {
|
Clone.prototype.setSize = function (size) {
|
||||||
// Keep size between 5% and 535%.
|
// Keep size between 5% and 535%.
|
||||||
this.size = MathUtil.clamp(size, 5, 535);
|
this.size = MathUtil.clamp(size, 5, 535);
|
||||||
self.renderer.updateDrawableProperties(this.drawableID, {
|
if (this.renderer) {
|
||||||
scale: [this.size, this.size]
|
this.renderer.updateDrawableProperties(this.drawableID, {
|
||||||
});
|
scale: [this.size, this.size]
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -140,9 +165,11 @@ Clone.prototype.setSize = function (size) {
|
||||||
*/
|
*/
|
||||||
Clone.prototype.setEffect = function (effectName, value) {
|
Clone.prototype.setEffect = function (effectName, value) {
|
||||||
this.effects[effectName] = value;
|
this.effects[effectName] = value;
|
||||||
var props = {};
|
if (this.renderer) {
|
||||||
props[effectName] = this.effects[effectName];
|
var props = {};
|
||||||
self.renderer.updateDrawableProperties(this.drawableID, props);
|
props[effectName] = this.effects[effectName];
|
||||||
|
this.renderer.updateDrawableProperties(this.drawableID, props);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -152,7 +179,9 @@ Clone.prototype.clearEffects = function () {
|
||||||
for (var effectName in this.effects) {
|
for (var effectName in this.effects) {
|
||||||
this.effects[effectName] = 0;
|
this.effects[effectName] = 0;
|
||||||
}
|
}
|
||||||
self.renderer.updateDrawableProperties(this.drawableID, this.effects);
|
if (this.renderer) {
|
||||||
|
this.renderer.updateDrawableProperties(this.drawableID, this.effects);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = Clone;
|
module.exports = Clone;
|
||||||
|
|
Loading…
Reference in a new issue