Flash Action Script 3 function scope
go_btn.addEventListener(MouseEvent.CLICK, getPlayerName);
var playerName;
function getPlayerName(开发者_Python百科e:MouseEvent)
{
playerName = playerName_txt.text;
}
trace(playerName);
Hi, is there any way to have this work. I want to update a variable outside the scope of the function.
Thanks
Put your code into a class, instead of a blob of code on a frame or on a MovieClip. All methods inside a class have easy access to any member variables defined on that class.
Give it a value outside of the function, then change it inside the function:
go_btn.addEventListener(MouseEvent.CLICK, getPlayerName);
var playerName:String;
playerName = "nono";
playerName_txt.text = "blah";
function getPlayerName(e:MouseEvent)
{
playerName = playerName_txt.text;
}
trace(playerName);
精彩评论