A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Duplicating an existing array

  1. #1
    Member
    Join Date
    Dec 2000
    Posts
    72
    Hi!

    i want to duplicate an existing array into another for backup, & i wanna use the new one for manupulation.

    i've done:
    // this is the array i wanna duplicate:
    letterArray = ["c", "o", "l", "d"]
    //this is how i m duplicating:
    var temp = letterArray

    but the prob is when i m manupilating temp, letterArray is also changing.
    PLease help...
    AJ

  2. #2
    Junior Member
    Join Date
    Jul 2001
    Posts
    24
    flash uses pointers to all objects, which is a good thing, trust me.

    here's some simple code to solve your immediate problem, this could be broken out into a function for reuse.

    //do it
    a1 = [1,2,3,4];
    a2 = new Array();
    for(z=0; z < a1.length; z++) {
    a2[z] = a1[z];
    }
    //test it
    a1[0] = 9;
    for(z=0; z < a2.length; z++) {
    trace(a2[z]);
    }

  3. #3
    Senior Member
    Join Date
    Jan 2001
    Posts
    472
    OK, I'll take a stab at a method to help:


    Array.prototype.duplicate = function() {
    var tempArray = new Array();
    for (var j = 0; j < this.length; j++) {
    tempArray[j] = this[j];
    }
    return tempArray
    }


    I believe this would do it, though I'm not entirely sure about declaring a new array inside the prototype-- sorry I can't test it right now. All you would need to create a duplicate of an array is:

    myArray = oldArray.duplicate();



    --tyard
    http://www.27Bobs.com

  4. #4
    Junior Member
    Join Date
    Jul 2001
    Posts
    24
    tyard's way does work and it's the proper way to handle it, especially if your going to be using it in more than one place.

  5. #5
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    You can use slice or concat to create a new array. Slice a whole array or concat with nothing will create a new array with the same content as old array.

    var temp=letterArray.concat();

  6. #6
    Senior Member
    Join Date
    Jan 2001
    Posts
    472
    Cool. Even better.


    --tyard
    http://www.27Bobs.com

  7. #7
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    Hi tyard

    (Sorry, not related with this thread.)

    Days ago, you post an swf about 3d balls animation. Very interesting. You promised to post the fla file. But, threads are easily drown here, I can not find that thread now.

    If you are ready to post the fla file, I hope I can see it. I believe there must be many interesting things for me to learn.

  8. #8
    Senior Member
    Join Date
    Jan 2001
    Posts
    472
    Hey, thanks for the interest. I'm nearly done commenting the code (probably finish that tonight), then I'm going to put together a help file tomorrow. The entire thing should be ready by Sunday, Monday at the latest. It's interesting-- in putting together some quick examples I found a number of different ways to use the smart clips from the way I intended. I hope other can do the same. Here's where the downloads will be:

    http://www.27Bobs.com/Bones/SphereAnimator/spheres.swf


    I'll also post in the 3D forum when it's complete-- let me know if you have any thoughts on the code when you get it! Thanks.


    --tyard
    http://www.27Bobs.com

  9. #9
    Member
    Join Date
    Dec 2000
    Posts
    72

    Lightbulb thnks

    Hi!

    thnks a lot all of u to help me out... i guess the concat() will be best for me in this case... i'll check it out...

    thnks
    AJ

  10. #10
    Senior Member
    Join Date
    Jan 2001
    Posts
    472
    Sorry, last non sequitur on this thread, promise.

    ericlin, just to let you know, the download is now ready at my site. All that's left is the help file, but you can download the interface and smart clip to get a peak at the code, if you'd like. I'm quite proud of it


    --tyard
    http://www.27Bobs.com

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