1

I have an AS3 function that runs when a URLRequest fails. I need to be able to use this function to assign global variables that I can then check against using other functions. How do I assign global variables inside of a function?

Thanks!

Edit:This is the variable (soundFile2Exist) I am trying to get outside of my function:

function onIOError(e:IOErrorEvent):void 
{

    var soundFile2exist = null;
    trace (soundFile2exist);
}

I am not using my code inside of a packge. Is there still a way to do this?

function playMusic(evt:MouseEvent):void
{
    channel = myMusic.play(songPosition);
    if (soundFile2exist != null) {
          channel2 = myMusic2.play(channel.position);
    }
    myTimer.start();
    btnPlay.mouseEnabled = false;
    trace (soundFile2exist);

}
7
  • I am strongly opposed to the entire premise of this question, that you would want to pass data between functions using global variables. Commented Sep 11, 2011 at 19:50
  • What do you mean? I may have mis-phrased my question. Commented Sep 11, 2011 at 19:52
  • There are many ways of sharing data between functions that do not involve global variables. Global variables are pretty much the worst way of accomplishing that task. Commented Sep 11, 2011 at 19:56
  • How do I share data between functions? Commented Sep 11, 2011 at 19:58
  • Your statement that "I am not using my code inside of a package" suggests to me that you don't know how to do object-oriented programming. AS3 is a strongly object-oriented language, so I suggest learning about that. Commented Sep 11, 2011 at 20:03

2 Answers 2

4

If your code is "not in a package" (like in a frame script on a timeline), any variables you declare on the same level, same scope, as your functions will be accessible to the functions in that scope, so you could do something in the lines of this:

var soundFile2exist;

function onIOError(e:IOErrorEvent):void {
   soundFile2exist = null;
}

function otherFunction():void {
   trace (soundFile2exist);
}

In other situations, static variables of a class can be used in place of global variables.

Sign up to request clarification or add additional context in comments.

4 Comments

Hmm... I tried this and it looks and the 2nd trace returns "undefined" rather than "null"
Sorry, there was an error in my example code, I had "var soundFile2exist = null" in the onIOError function, making it a local variable to that function. I edited the example, now it should work better.
upvote because this is the best answer to the question, but I don't recommend this way of programming. Timeline code is almost always a bad idea (AS2 was even worse in this regard because it required it; at least with AS3 you don't have to put code on the timeline).
Is there any way you can access such variables by name in variable? Like globals['soundFile2exist'] = null or something?
1

The most common approach is to create a Singleton class which you can then reference from anywhere. This basically sets up a class where the only way to get access to an instance of that class is via a controlled static method that returns one and only one instance. So whenever you say MySingleton.getInstance() you always end up with the same object.

Grant Skinner has a quick writeup of how to implement a Singleton in AS3: http://gskinner.com/blog/archives/2006/07/as3_singletons.html

3 Comments

The obvious solution to problems with your code not being in a package is to put it in a package. Is there some reason why you can't do that?
Incidentally, I don't want to down-vote this answer because I used to use Singletons occasionally, but I've learned a lot about the downsides of Singletons and once I started using dependency injection I found I never needed Singletons at all.
Sure, but someone asking this question is probably not at a point where impleneting dependency injection would be non-trivial. This seemed better than to simply say "Create a class with all-static methods." ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.