A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Pushing in Arrays

  1. #1
    Senior Member
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    145

    Pushing in Arrays

    Hi all,

    I think this is quick question. Below is a function that I'm trying to get to push new values into an array. It works the first time but then returns an undefined thereafter.

    code:

    function dollyFunction(target) {
    if (functionPass ==0 ) {
    var sickBayArray:Array = new Array();
    //trace(sickBayArray);
    functionPass = 1;
    }
    var thisTarget:String = target;
    trace(eval(thisTarget));
    if (eval(thisTarget)._currentframe != 2) {
    eval(thisTarget).gotoAndStop(2);
    } else {
    eval(thisTarget).gotoAndStop(1);
    }
    var pushVal:String = thisTarget.substring(0,2);
    sickBayArray.push(pushVal);
    trace("pushVal "+pushVal);
    trace(thisTarget.substring(0,2));
    trace("the array contains "+sickBayArray.toString());

    }



    Am I missing something here when using the push method?

    Thanks heaps

    ss

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    The problem (or one of them, at least) is your use of the keyword var on this line:

    var sickBayArray:Array = new Array();

    var is used to indicate that a variable is local to a function (and will no longer be used when the function exits). This means that the array will no longer exist when you exit the function, and can't be reused to push additional items onto it in subsequent calls. Nor can it be referenced outside of that function, as you probably intend to do.

    One way to fix this is to remove the word var from that line.

    If I were to rewrite the code, I would initialize the array outside the function, as in the following code, in which I'm also cleaning up a few other things...

    Another possibility is to pass the array to the function as a parameter.

    code:


    // initialize this outside the function
    sickBaryArray = []; // [] is the same as new Array()

    function dollyFunction(target) {
    var mc = eval(target); // do this evaluation once, and then reuse it...
    if (mc._currentframe != 2)
    {
    mc.gotoAndStop(2);
    } else {
    mc.gotoAndStop(1);
    }
    sickBayArray.push(target.substring(0,2));
    }

    Last edited by jbum; 10-18-2004 at 04:26 AM.

  3. #3
    Senior Member
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    145
    Thanks heaps for that.

    Just getting used to using var and trying to be more formal in my scripting - got away with pretty sloppy coding in AS1

    Thanks again,

    ss

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center