开发者

as3 How can a function use variable from another function?

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 e开发者_如何学Goxample:

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

init();

trace(test);

I get an error:

1120: Access of undefined property test.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜