exportRoot returning inconsistent number of children

Explorer ,
Sep 18, 2019 Sep 18, 2019

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.

 

  • If I try to access exportRoot.numChildren from code on the main timeline it returns 0 children.
  • If I try to access exportRoot.numChildren by adding code to the fnStartAnimation function Adobe Animate creates it returns 0 children
  • if I instead add console.log(exportRoot.children) to the fnStartAnimation function, it returns an array that does actually have all the children in it, but still claims to have 0 children whether I ask for exportRoot.numChildren or exportRoot.children.length
  • if I bring up the dev console in chrome and acces exportRoot.numChildren once the animation is running it returns the correct number for however many objects I've added to the stage

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.

Views

115

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Sep 18, 2019 Sep 18, 2019

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 18, 2019 Sep 18, 2019

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);
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Sep 18, 2019 Sep 18, 2019

Copy link to clipboard

Copied

LATEST
you’re welcome

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines