var fruit:Array = new Array();
var frName:String;
var i:Number;
save_btn.addEventListener(MouseEvent.CLICK, storeName);
function storeName(Event:MouseEvent)
{
frName = name_txt.text;
fruit[i] = frName;
i++;
}
detail_btn.addEventListener(MouseEvent.CLICK, dispName);
function dispName(Event:MouseEvent)
{
for(i=0; i<=1; 开发者_C百科i++)
{
trace(fruit[i]);
}
}
The script has two buttons: one for saving the text in a fruit
array and the other for displaying the text.
However, when I click the display button, the script shows undefined as output in actionscript. Please help.
When you defined your "i" variable you never set a value... e.g. it is "undefined". Just set it to zero.
var i:Number = 0;
declaring i as a global variable is making things difficult. Perhaps try rewritting like so:
var fruit:Array = new Array();
save_btn.addEventListener(MouseEvent.CLICK, storeName);
function storeName(Event:MouseEvent){
fruit.push(name_txt.text);
}
detail_btn.addEventListener(MouseEvent.CLICK, dispName);
function dispName(Event:MouseEvent){
for(var f:String in fruit){
trace(f);
}
}
精彩评论