Copy link to clipboard
Copied
Here's my scenario: I'm trying to use nominalBounds to store a width and height property on all the objects I've added to the stage at the start of my animation. To do this I have this function
function storeBounds(i)
{
var bounds = i.nominalBounds;
i.width = bounds.width;
i.height = bounds.height;
}
which works fine when I feed it one object at a time, but I want it to just automatically run on everything that's on the stage. So my first thought was to simply use a for loop and run this function on all the children of exportRoot, which is where I've run into my issue.
So my question is, what am I doing wrong here? Why is exportRoot returning conflicting information about it's number of children. Is there a better way I should be going about this? I realize I could just make an array and manually log every object I add to the stage into it and then run my storeBounds function on that array, but it seems like a lot of unneccessary work on my part when there is already an array of all the objects on stage in exportRoot.
Any advice is greatly appreciated.
Thank you.
Copy link to clipboard
Copied
use a ticker to wait for one tick event before trying to access all the children present at the start.
Copy link to clipboard
Copied
That worked, thank you @kglad.
For anyone else who might encounter this post via google, I also discovered this solution
function waitForExportRoot()
{
if(exportRoot.numChildren>0)
{
for(let i in exportRoot.children)
{
//do something to each child of exportRoot
}
cancelAnimationFrame(waitForExportRoot);
return;
}
requestAnimationFrame(waitForExportRoot);
}
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now