0

I have created an array with fixed size of 5, but when I try to insert element into array, the element is inserted into 6th position. Can anyone tell me how to insert value from the first place of array?

PS:Since the elements are randomly inserted, so I couldn't do something like this:songTitles[5] = "Happy Birthday";

Some references are as follows:

myRes = new Array(5);

     if(e.keyCode == 65){

        myRes.push(word1.getLabel());
        trace(myRes);
    } else if (e.keyCode == 83) {

        myRes.push(word2.getLabel());
        trace(myRes);

    } else if (e.keyCode == 68) {

        myRes.push(word3.getLabel());
        trace(myRes);

    } else if (e.keyCode == 70) {

        myRes.push(word4.getLabel());
        trace(myRes);

    } else if (e.keyCode == 71) {

        myRes.push(word5.getLabel());
        trace(myRes);

    }

The Result I get:

,,,,,A ,,,,,A,S ,,,,,A,S,F

2
  • Is there a reason why you gave it a length of 5 when declaring the Array? Commented Sep 1, 2013 at 12:28
  • It is required by the project criteria. Commented Sep 1, 2013 at 14:19

1 Answer 1

2

Try myRes = new Array(); When you create an array with a fixed size flash fills it with empty values. If you don't want your arrays length to exceed 5, just insert a conditional checking the length into your keyboardevent handler function.

Sign up to request clarification or add additional context in comments.

3 Comments

yeah, I can do it that way as well. could I ask u one more question?Basically, I got three classes -- main , function1 and function2(array). I defined the value of an array in main. In function1, I need to call function2 with parameter of the array in main. So how can I pass array from main to function1?
Calling your classes functions might be misleading. Also try using Capital letters for classes. function1 would need a reference to the array declared in main, you can pass it to the class beforehand. Do you really have to call function2 from function1? Simplifying your classes might be a bit of work now, but save you time in the end.
declare it in function1: public var mainArray:Array; give it value in main: function1.mainArray = myRes;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.