0

Obviously I'm new to as3...Can someone please explain to me how I can use the variable from one function in another function?

For example:

function init():void {
 var test:Number = 1;
}

init();

trace(test);

I get an error:

1120: Access of undefined property test.

1 Answer 1

1

Either define the variable outside of the function:

var test:Number = 0;

function init():void
{
    test = 1;
}

init();

trace(test); //output: 1

Or return the value from the init() function like this:

function init():Number
{
    var test:Number = 1;
    return test;
}

trace(init()); //output: 1

Note:

Normally you'd just do:

function init():Number
{
    return 1;
}

But I did the above for the sake of explanation.

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

2 Comments

Thanks buddy, that's the quickest answer you've replied me so far! i've been timing you lol
Lol. Hopefully my boss hasn't ;)

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.