Flash Tips: How to Copy an Array

{ December 20th, 2007 }

I’ve been using Adobe Flash for many years to develop engaging user experiences, for both the web and desktop. During this time I’ve had my share of problems. Flash has always had a weak help file, specifically the ability to search the help file. This has made it much harder to find solutions to problems, unless you know the specific terminology to search for beforehand. Today’s post is the first in a series of Flash tips, quick guides to help you develop in Flash.

How to Copy an Array

When adding the ability to drag and drop cells within ConceptShare version 2, I decided the best method to store cell placement was to use multidimensional array. This worked perfectly at first, but mysterious problems started appearing when toggling full screen, or changing between layouts. When first encountering this error I didn’t think for a moment the issue was copying the array, and spent days on end looking for an answer. The problem did turn out to be copying the array for future use. Normally you would do something like this:

array1 = [a,b,c];
array2=array1;

You would assume that array2 is now a copy of array1 and can be edited independently. Well, that was not the case. array2 is a reference of array1. So any changes made to array2 are also made on array1. In order to get around this you must use the slice function:

array2 =array1.slice();

This only works for single dimension arrays, multi-dimension arrays require a slice to be called on each array contained within. So that’s how to copy an array in Flash.

Next time on Flash tips, the file reference class.

If you enjoyed this post, make sure you subscribe to our web feed!

Categories: Development ~ ~ Trackback

2 Responses to “Flash Tips: How to Copy an Array”

  1. 1
    PHP Coding School » Blog Archive » php tips [2007-12-21 02:04:22]

    […] Flash Tips: How to Copy an Array By Chris D’Aoust Today’s post is the first in a series of Flash tips, quick guides to help you develop in Flash. How to Copy an Array. When adding the ability to drag and drop cells within ConceptShare version 2, I decided the best method to store cell … Thought Balloons - http://www.thoughtballoons.net […]

  2. 2
    Flash Tips: The File Reference Class | Thought Balloons

    […] Contributors « Flash Tips: How to Copy an Array […]

Leave a Reply