Sam
Sam

Reputation: 501

How to use an array in actionscript?

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; 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.

Upvotes: 0

Views: 311

Answers (2)

Matthew Cook
Matthew Cook

Reputation: 178

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);
  }
}

Upvotes: 1

scunliffe
scunliffe

Reputation: 63580

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;

Upvotes: 1

Related Questions