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

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

bt i put the value in for loop also for i=0 why twice I have to give the value??
When you are populating the array originally... The first index you are storing on is "undefined"... After that, if you're lucky (I haven't tested) the ++ increments from undefined to 1. By ensuring the values are stored correctly, they'll extract correctly too ;-)
1

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

2 Comments

i m sorry bt i have jst started AS 3.0 and i don't know the push command etc. as they are advanced technique i think so...
Adobe's Livedocs are an excellent reference: livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3 it's often easier to use built-in functionality, rather than try and implement your own looping, adding, etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.