I am struggling with assigning a sprite to an Image element using a script. I am pulling in a sprite through an attribute on the script, like so:
MyScript.attributes.add('myAttributeSprite', {type: 'asset', assetType: 'sprite'});
When I assign the sprite to the image element,
this.myEntity.element.sprite = this.myAttributeSprite;
I get this error in the console:
Which is pointing to this chunk in the engine:
Object.defineProperty(ImageElement.prototype, "sprite", {get:function() {
return this._sprite;
}, set:function(value) {
if (this._sprite === value) {
return;
}
if (this._sprite) {
this._unbindSprite(this._sprite);
}
if (this._spriteAsset) {
var spriteAsset = this._system.app.assets.get(this._spriteAsset);
if (spriteAsset && spriteAsset.resource !== value) {
this.spriteAsset = null;
}
}
this._sprite = value;
if (this._sprite) {
this._bindSprite(this._sprite);
if (this._textureAsset) {
this.textureAsset = null;
}
}
if (this._sprite && this._sprite.atlas && this._sprite.atlas.texture) {
this._renderable.setParameter("texture_emissiveMap", this._sprite.atlas.texture);
this._renderable.setParameter("texture_opacityMap", this._sprite.atlas.texture);
} else {
this._renderable.deleteParameter("texture_emissiveMap");
this._renderable.deleteParameter("texture_opacityMap");
}
if (this._sprite) {
this._spriteFrame = pc.math.clamp(this._spriteFrame, 0, this._sprite.frameKeys.length- 1);
}
this._updateSprite();
}});
The _sprite.frameKeys in the second to last line is where the problem comes in. Not sure how I can get the Image element to recognize this is a simple sprite with 1 frame. I tried many different avenues and can’t seem to work around this issue.
Thanks!