FoxLift
FoxLift

Reputation: 433

AS3 create a variable in root from within a function

I have a fairly big swf right now with a bit of coding already. Most vars are created in the root, but now I have a problem.

I want to reload the flash swf (reset), and for that, I need to create a function that destroys all the vars and another one that creates them. At the moment, I have a javascript function that reloads the page, but that really isnt a good solution.

The problem is that when I create a var inside a function, it doesn't get created in "MovieClip(root)", and instead is only related to the function, thus rendering my swf unable to work.

Is there a way to create vars in MovieClip(root) from within a function? Or is there an alternative to what I'm trying to do?

EDIT: Added some example code.

function SetVar():void{
var test:String= new String("foobar");
}

SetVar();

trace(test);

...and the output is:

Scene 1, Layer 'Layer 1', Frame 1, Line 7   1120: Access of undefined property test.

Which is normal, because the "var test" is not global, so it was lost when the function ended. I want to make it so the function "SetVar()" adds the vars to the root, or global.

Upvotes: 0

Views: 1384

Answers (3)

Josh
Josh

Reputation: 8149

You need to read up on how scope works.

Basically:

  1. An object declared within another object (be it a Class, Function, Object, or Loop), is only available within that specific object or loop iteration.
  2. Object scope is inherited by children, not by parents. So a function within a class has access to an object declared within that class, but a class does not have access to an object declared within a function
  3. A parent (or any other object) can access objects declared within child classes, but only if it is a public object

So looking at those basic rules (they are very, very basic. If you are just starting out, I urge you to do some proper research into object scope in OOP. It is the basis of everything you will do in dozens of languages), you are declaring an object in a function and trying to access it from outside that function. This breaks Rule #1 from above.

Instead, try this:

var test:String;
function setVar():void{
    this.test = 'foorBar';
}

trace(test); //output: null (undeclared)

setVar();

trace(this.test); // output: fooBar

Looking at this, I did two things:

  1. I moved the declaration of test into global space, meaning any object in that object will have access to it
  2. I renamed SetVar to setVar. This has nothing to do with your question, but in AS3, the standard naming conventions dictate you use lowerCaseCamelCase for all objects (including functions), UpperCaseCamelCase for all Class names, and lowercasename for all package names. Again, unrelated but it is good to learn.

Now, ideally, you would probably want to do that setVar function slightly differently. To allow for better abstraction (basically making your code as generic an reusable as possible), you would want to return the value from the function rather than manually set the variable in the function.

var test:String;
var anotherTest:String;
function setVar():String {
    return 'foorBar';
}

this.text = setVar();
this.anotherTest = setVar();    

trace(this.test); // output: fooBar
trace(this.anotherTest); // output: fooBar

So that allows you to use that function with any String variable imaginable. Obviously, that is not very useful here since it doesn't do any logic. But I am sure you can see how that could be expanded with more code to make it more dynamic and much more useful

EDIT: As an afterthought, I used the this keyword. In AS3 (and a few other languages), this refers to the scope of the current class (or current frame, in case of timeline frame coding). So this.test refers to a variable test declared in the scope of the frame or class.

Upvotes: 3

Vikram Deshmukh
Vikram Deshmukh

Reputation: 15616

Since you are declaring the variable within the function, its scope is restricted to that function only.

Try declaring the variable outside the function and initializing it in the function instead. You should then be able to access it from root.

But if you wish to declare a variable on root from within a function (highly unusual requirement) then you can try doing:

document["variableName'] = value;

or

root["variableName'] = value;

inside the function.

Upvotes: 0

NappingRabbit
NappingRabbit

Reputation: 1918

I am not entirely sure what you are looking for because there is no code associated with your question. However I will impart a bit of information I feel relates to the subject.

if you declare your variables in the class then you can reference them from a function as such:

package{
    import flash.display.MovieClip;

    public class DocumentClass extends MovieClip{

        public var example:String = 'dog';

        public function DocumentClass(){
            trace(example); // dog
            testFctn();
            trace(example); // frog
        }

        public function testFctn(){
            example = 'frog'
        }
    }
}

if you want to reference the variable of a parent class this.parent['variableName'] can be useful too. or a sibling of your working class sharing a parent class, this.parent['childClass']['variableName'] ...

Upvotes: 0

Related Questions