Skip to main content
Participant
October 30, 2019
Answered

HTML5 Canvas using global script to add movie clips to the stage?

  • October 30, 2019
  • 2 replies
  • 1072 views

Hi! I'm working my way through learning HTML5, and I'm trying to figure out global scripting. Is there a way to add a movie clip from the library to the stage using a global script? Currently I would do something like this in the actions panel:

 

var _this = this;

var MyMoveClip = new lib.myMovieClip_mc();

function addMovieClip() {
_this.addChildAt(MyMoveClip , 1);
}

 

But when I try to do a similar thing with global scripting:

 

function GlobalScript (){

var MyMoveClip = new lib.myMovieClip_mc();

exportRoot.addChildAt(MyMoveClip, 1);

}

 

I get an error saying the script can't find "lib"

Is this something I will just have to do in the actions panel?

 

Thanks!

This topic has been closed for replies.
Correct answer ClayUUID

No, don't do that. If you try to do anything before the Animate content is fully initialized, it either won't work or will break things. The easiest way to guarantee that you don't call your global function prematurely is to just call it in the first frame of Animate's root timeline.

2 replies

Participant
October 31, 2019

Ok I did some digging and I need to add this line of code to define lib

 

var lib = AdobeAn.getComposition(AdobeAn.bootcompsLoaded[0]).getLibrary();
ClayUUIDCorrect answer
Legend
October 31, 2019

No, don't do that. If you try to do anything before the Animate content is fully initialized, it either won't work or will break things. The easiest way to guarantee that you don't call your global function prematurely is to just call it in the first frame of Animate's root timeline.

Participating Frequently
October 31, 2019

Hey mate, you can try using ES6 javascript fat arrow functions:

 

addingClip = () => {
	var myClip = new lib.LibrarySymbol();
	stage.addChild(myClip);
}
addingClip();
Legend
October 31, 2019

No, don't use fat arrow functions (especially in a situation like this where it's nonsensical to do so; fat arrows are supposed to be used for anonymous functions, not defining named functions). They don't work under IE11.

 

Also, adding clips directly to stage is different from adding things to exportRoot. exportRoot is the root timeline. stage is exporRoot's parent container.

 

58911893, you aren't trying to call this function directly in the page, are you? If you are, you're probably calling it before lib is defined.

Participant
October 31, 2019

Yes I was trying to call it from the page, when is lib defined?