Highlighted

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

Community Beginner ,
Oct 30, 2019

Copy link to clipboard

Copied

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!

Adobe Community Professional
Correct answer by ClayUUID | Adobe Community Professional

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.

TOPICS
Code, How to

Views

123

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

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

Community Beginner ,
Oct 30, 2019

Copy link to clipboard

Copied

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!

Adobe Community Professional
Correct answer by ClayUUID | Adobe Community Professional

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.

TOPICS
Code, How to

Views

124

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
Oct 30, 2019 0
Community Beginner ,
Oct 30, 2019

Copy link to clipboard

Copied

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

 

addingClip = () => {
	var myClip = new lib.LibrarySymbol();
	stage.addChild(myClip);
}
addingClip();

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
Reply
Loading...
Oct 30, 2019 0
Adobe Community Professional ,
Oct 31, 2019

Copy link to clipboard

Copied

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.

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
Reply
Loading...
Oct 31, 2019 0
Community Beginner ,
Oct 31, 2019

Copy link to clipboard

Copied

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

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
Reply
Loading...
Oct 31, 2019 0
Community Beginner ,
Oct 31, 2019

Copy link to clipboard

Copied

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

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
Reply
Loading...
Oct 31, 2019 0
Adobe Community Professional ,
Oct 31, 2019

Copy link to clipboard

Copied

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.

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
Reply
Loading...
Oct 31, 2019 0