Sunday, 30 May 2010

Cloning arrays

On the other day I had the need to copy an Array. It was not the first time, but this time I had to make a temporary copy of another array, and put some new data in it and make some calculations.

In this process my need to change the original Array, was none.

The problem is that if you make something like this:

var myArray:Array=new Array("Blue", "Yellow");
var myCopyArray:Array=new Array();


... it works to some extent. You can also do this with concat or slice commands.
This method makes a shallow copy, which means that the copy only stores the reference to the original values, that is, it only stores the memory reference where the values are stored. This means that if you change one of the arrays, the other will change also... This was not what I meant.

I needed to make a hard copy of the array, change it, without changing the original. During the research, I found this generic solution presented in a webpage by Adobe at the following link:

As always I transcript the code to here and make a brief explanation.

So the function looks like this:

import flash.utils.ByteArray;

function clone(source:Object):*
    var myBA:ByteArray = new ByteArray();
    myBA.position = 0;
Basically what this does is it goes to the memory and gets what is stored in the place where the original Array is referenced. To get the original data and not the reference, it uses the ByteArray class and stores the data in the myBA:ByteArray object. The ByteArray class insures that it copies whatever is stored in the original Array, i.e, Numbers, String, DisplayObjects, whatever...

Pretty handy...

Friday, 21 May 2010

How to erase a element in a Array

I came across this problem recently.

The Array has some methods, and the most common are PUSH, that lets you put an element in a Array. This element will go to the end.

And then you have the POP method, but this only gets the last element in a Array.

Now the question is "How do I delete an element giving the position for that element"?

It's a bit trickier, and it's done using another method called SPLICE.

The syntax is:
splice(startIndex:int, deleteCount:uint... values)

This means:
- startIndex -> Initial position were you want to delete. It's the index position of the Array;
- deleteCount ->This is how many elements you want to delete, that includes the index position you specified in startIndex. If you say 1, then it will delete the index position that you've specified. If you say 2, then it's the index position, and the next element... and so on.

As an example:

var myList:Array=new Array("Blue", "Red", "Green", "Black,"White");

trace(myList); //Blue, Red, White

//Green is Index number 2, and then deletes two elements including Green, which are Green and Black

Tuesday, 4 May 2010

Instantiate an SWF as a Class

At the company where I work we add the need to make several SWF that needed to come up, in different timings and according to user navigation. But since we wanted to keep the final application as light as possible, we had the need not to instantiate all the external swf at once.

After some research we found and implemented the following solution: instantiate external swf's as separate classes:

  import flash.display.MovieClip;
  import flash.display.Loader;
  public class Load extends MovieClip
     var storeLoadedObject:Class; //Self explained ;)
     var loader:Loader;
     public function Load():void{
         loader=new Loader();
         loader.load(new URLRequest("example.swf"));
         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeObjForEdit);

     public function completeObjForEdit(evt:Event):void{
         var className:String="com.gameRoot"; //Document class of loaded file
          storeLoadedObject=loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;

     public function makeLotsOfCopies():void{
        for(var i:uint=0; i<20;i++){
           var copy:MovieClip=new storeLoadedObject();

And that's it.