Skip to main content
Participating Frequently
September 25, 2018
Answered

Canvas Global Scripts

  • September 25, 2018
  • 2 replies
  • 2812 views

I'm new to Canvas in Animate. I have the basics down I think, but I'm having trouble with Global scripting. It may be that I'm thinking of the Global Scripts window incorrectly, but my thought was that it can be used to write code that will be accessed throughout the project. My hope was to use it to declare global variables and functions that I want to later call on other frames in the file. However, I can't seem to get any of my variables to recognize between the global script window and other frames. I was trying to learn how to use this window, but found the tutorials a little lacking. The help files didn't help me understand it any better either. Could someone help me understand what type of code can be used in the Global Scripts window? I saw on one site the use of console. This got me thinking only strict JS could be used. But I couldn't even declare variables there. Any help or advice would be greatly appreciated! Thanks everyone in advanced!

This topic has been closed for replies.
Correct answer kglad

you can't target on-stage objects with a global script until the object exists.  otherwise, there's no problem.

eg, if you wanted to do something with objects in frame 33 using a global script, on frame 33 use something like:

frame33_globalF.bind(this)();

in your global script:

function frame33_globalF(){

// you can reference frame 33 objects here

}

2 replies

Legend
September 25, 2018

You should post what you've attempted. I just tried it and it worked fine. I put this code in the Global Script window:

var bob = "your uncle";

function mytest() {

    alert("success!");

}

Then this script in the first frame of the root timeline:

alert(bob);

mytest();

And got two popups with the expected content when I ran the page.

Participating Frequently
October 9, 2018

Thanks for the response Clay,

I tried that, and a little more elaborate versions of it but couldn't get it to work. Specifically, I tried to target objects on the stage but I just get blank pages.

For example, I wanted to globally assign the "this" object to the variable "_this" because some of my code on the frames worked only when I used this variable instead of just saying something like "this.objectname.visible=false;", where objectname is the name of the object on the screen. But when I did this, none of the code worked. I just got a blank screen. Is there a proper way to target objects from the global scripts window that I'm missing?

Thank you in advanced!

Legend
October 9, 2018

prashantsavalia  wrote

For example, I wanted to globally assign the "this" object to the variable "_this" because some of my code on the frames worked only when I used this variable instead of just saying something like "this.objectname.visible=false;", where objectname is the name of the object on the screen. But when I did this, none of the code worked. I just got a blank screen.

The global variable exportRoot is automatically created by Animate and points to the root timeline.

When you get a white screen, 99% of the time that means there was a runtime error. Open the browser's developer console and view the error message.

kglad
Community Expert
Community Expert
September 25, 2018
Participating Frequently
October 9, 2018

kglad,

Thank you, but I started there and it was very limited on formatting and object targeting. I usually start with the AdobeX files before hitting the forums. I appreciate the advice though. Thank you.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
October 9, 2018

you can't target on-stage objects with a global script until the object exists.  otherwise, there's no problem.

eg, if you wanted to do something with objects in frame 33 using a global script, on frame 33 use something like:

frame33_globalF.bind(this)();

in your global script:

function frame33_globalF(){

// you can reference frame 33 objects here

}