English 中文(简体)
How to pass variables into inline functions in Action Script 2
原标题:

I have the following function, but I can t seem to get the myVar variable into the inline function. What am I doing wrong here? What I would like to have happen is when I click on myMc, it should print myVar to the console ("hello computer").

function doSomething():Void
{   
    myVar = "hello computer";

    myMc.onRelease = function(){
        trace(myVar); //prints as "undefined"
    }
}

ps. - I cannot declare myVar as a global or static variable because in the real code, I m parsing XML and the myVar is constantly changing.

最佳回答

This is a scope issue - when you apply an onRelease function like this in as2, the scope of the function is the MovieClip you apply the function to, not the calling function.

Because you are using AS2 and MovieClip is dynamic, you can assign the variable to the MC directly:

function doSomething():Void
{   
    myMc.myVar = "hello computer";

    myMc.onRelease = function(){
        trace(this.myVar);
    }
}
问题回答

Try declaring myVar with the var keyword:

var myVar = "hello computer";




相关问题
Scope with a self-invoking function in Javascript

Take below code iterates over 6 input buttons and attaches an onclick event to every button that alerts the index number of the respective iteration: for (var i = 1; i < 6; ++i) { var but = ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

定型语言是否具有范围概念?

定型语言是否具有范围概念? 在我看来,如果职能参数被放在职能执行之前的位置上,这些参数就会以不正统的方式出现。

"Global" variable scope in PHP

I have a section of code like the following: ---- file.php ---- require_once("mylib.php"); function($a,$b) { $r = $_GLOBALS[ someGlobal ]; echo $r; } ---- mylib.php ---- $_GLOBALS[ ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

热门标签