I have created a custom script that fades a popup in and out on a button click. When I add this script to more than 1 button in my scene the fading animation only works on the entity that is lowest in the hierarch.
I have 2 buttons; b1 and b2, where b1 is higher in the hierarchy, and 2 modals; m1 and m2. When I click b1 I expect m1 to fade in/out and when I click b2 I expect m2 to fade in/out. However b1 will just enable m1 and not do the fade action.
**To note this is all created on a 2D screen.
here is the script code:
var ModalAnimation = pc.createScript('modalAnimation');
ModalAnimation.attributes.add('Modal', {type: 'entity'});
ModalAnimation.attributes.add('BasicFade', {type: 'entity', array: true});
ModalAnimation.attributes.add('CloseButton', {type: 'entity'});
ModalAnimation.attributes.add('OpacityMultiplier', {type: 'number', default: 1.2});
ModalAnimation.attributes.add('LowestOpacity', {type: 'number', default: 0.1});
ModalAnimation.attributes.add('HighOpacity', {type: 'number', default: 0.97});
var fadeOutModalFlag = false;
var fadeInModalFlag = false;
var basicFade;
var modal;
var opacityMultiplier;
var lowestOpacity;
var highOpacity;
// initialize code called once per entity
//TODO: lock all buttons once popups are open
ModalAnimation.prototype.initialize = function()
{
basicFade = this.BasicFade;
modal = this.Modal;
opacityMultiplier = this.OpacityMultiplier;
lowestOpacity = this.LowestOpacity;
highOpacity = this.HighOpacity;
this.entity.button.on('click', function()
{
if (this.Modal.enabled)
{
fadeOutModalFlag = true;
}
if(!this.Modal.enabled)
{
for(let i = 0; i < basicFade.length; i++)
{
basicFade[i].element.opacity = this.LowestOpacity;
}
this.Modal.enabled = true;
fadeInModalFlag = true;
}
}, this);
this.CloseButton.button.on('click', function()
{
this.Modal.enabled = false;
}, this);
};
ModalAnimation.prototype.DetermineFade = function()
{
if (this.Modal.enabled)
{
fadeOutModalFlag = true;
}
if(!this.Modal.enabled)
{
for(let i = 0; i < basicFade.length; i++)
{
basicFade[i].element.opacity = lowestOpacity;
}
this.Modal.enabled = true;
fadeInModalFlag = true;
}
};
ModalAnimation.prototype.FadeOutModal = setInterval(function()
{
if(fadeOutModalFlag && !fadeInModalFlag)
{
var tempOpacity;
for(let i = 0; i < basicFade.length; i++)
{
basicFade[i].element.opacity /= opacityMultiplier;
tempOpacity = basicFade[i].element.opacity;
}
if(tempOpacity < lowestOpacity)
{
modal.enabled = false;
fadeOutModalFlag = false;
}
}
}, 10);
ModalAnimation.prototype.FadeInModal = setInterval(function()
{
console.log()
if(fadeInModalFlag && !fadeOutModalFlag)
{
var tempOpacity;
for(let i = 0; i < basicFade.length; i++)
{
basicFade[i].element.opacity *= opacityMultiplier;
tempOpacity = basicFade[i].element.opacity;
}
if(tempOpacity >= highOpacity)
{
fadeInModalFlag = false;
//reset
}
}
}, 10);
I suggest to debug your script with the console of your browser to find out what’s wrong. Maybe your global variables are not used correctly, since all buttons use the same variables.
I am a little confused as to how the script would still be working overall and show the correct modal, but not doing the animations?
I am not familiar with how PlayCanvas sets up it’s entities but why would a script object on 2 different entities have references that are scrambled? I am expressly setting each individual entity that I want to fade (stored in the basicFade attribute).
Its not giving errors but if you put breakpoints in at line 59 or 78 and push one of the buttons, it wont execute the logic in the if statement because the this.flag is undefined
On button click the modal ui should fade in to at least .97 opacity, it is not