Dear all,
I am trying to understand AS3 and to be honest it has been difficult for me, specially because I am not a programmer. As part of my learning process I wanted to understand how to attach movies from the library. Following some examples from here and from other forums I did this little experiment. Basically I am reading from a multidimensional array the names of the movies in the library and then I erase all them. I will appreciate if you tell me if I am doing it well and how I can improve it. This is the code.
import flash.utils.getDefinitionByName;
[COLOR=“Red”]//Event for a button which will erase the instances[/COLOR]
borrar.addEventListener(MouseEvent.MOUSE_DOWN, borrarGrilla);
[COLOR=“Red”]//Matriz is my multidimensional array[/COLOR]
var matriz:Array = new Array ();
matriz = [
[“rojo”, “amarillo”, “azul”],
[“amarillo”, “azul”, “rojo”],
[“azul”, “rojo”, “amarillo”],
[“rojo”, “amarillo”, “azul”],
[“amarillo”, “azul”, “rojo”],
[“azul”, “rojo”, “amarillo”],
];
[COLOR=“Red”]//contenedor is a container for the instances[/COLOR]
var contenedor:Array = new Array();
[COLOR="#ff0000"]//filas holds the rows [/COLOR]
var filas:Array = new Array();
[COLOR=“Red”]//nombre is the name of the new instance [/COLOR]
var nombre:String;
[COLOR="#ff0000"]//Loop to get the name of the instances[/COLOR]
for (var i:Number = 0; i < 6; i++) {
for (var j:Number = 0; j < 3; j++) {
nombre = matriz*[j];
crearTile(nombre,32i,32j,i,j);
}
contenedor *= filas;
filas=[];
};
[COLOR="#ff0000"]//function to show the instances[/COLOR]
function crearTile(nombre:String,xPos:Number,yPos:Number,fila:Number,col:Number):void {
var tile:String = nombre;
var classRef:Class = getDefinitionByName(tile) as Class;
var instance:Object = new classRef();
filas [j]=addChild(DisplayObject(instance)).name;
instance.x= xPos;
instance.y= yPos;
}
[COLOR="#ff0000"]//Function to erase all the instances[/COLOR]
function borrarGrilla(event:MouseEvent){
for (var i:Number = 0; i < 6; i++) {
for (var j:Number = 0; j < 3; j++) {
nombre = contenedor*[j];
removeChild(DisplayObject(getChildByName(nombre)));
}
}
}
Thanks in advance