• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

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

TOPICS
Code , How to

Views

655

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

correct answers 1 Correct answer

LEGEND , Oct 31, 2019 Oct 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.

Votes

Translate

Translate
Community Beginner ,
Oct 30, 2019 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();

Votes

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
LEGEND ,
Oct 31, 2019 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.

Votes

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

Copy link to clipboard

Copied

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

Votes

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
Community Beginner ,
Oct 31, 2019 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();

Votes

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
LEGEND ,
Oct 31, 2019 Oct 31, 2019

Copy link to clipboard

Copied

LATEST

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.

Votes

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