costa
05-13-2007, 02:59 PM
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;
//Event for a button which will erase the instances
borrar.addEventListener(MouseEvent.MOUSE_DOWN, borrarGrilla);
//Matriz is my multidimensional array
var matriz:Array = new Array ();
matriz = [
["rojo", "amarillo", "azul"],
["amarillo", "azul", "rojo"],
["azul", "rojo", "amarillo"],
["rojo", "amarillo", "azul"],
["amarillo", "azul", "rojo"],
["azul", "rojo", "amarillo"],
];
//contenedor is a container for the instances
var contenedor:Array = new Array();
//filas holds the rows
var filas:Array = new Array();
//nombre is the name of the new instance
var nombre:String;
//Loop to get the name of the instances
for (var i:Number = 0; i < 6; i++) {
for (var j:Number = 0; j < 3; j++) {
nombre = matriz[i][j];
crearTile(nombre,32*i,32*j,i,j);
}
contenedor [i]= filas;
filas=[];
};
//function to show the instances
function crearTile(nombre:String,xPos:Number,yPos:Number,fi la: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;
}
//Function to erase all the instances
function borrarGrilla(event:MouseEvent){
for (var i:Number = 0; i < 6; i++) {
for (var j:Number = 0; j < 3; j++) {
nombre = contenedor[i][j];
removeChild(DisplayObject(getChildByName(nombre))) ;
}
}
}
Thanks in advance
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;
//Event for a button which will erase the instances
borrar.addEventListener(MouseEvent.MOUSE_DOWN, borrarGrilla);
//Matriz is my multidimensional array
var matriz:Array = new Array ();
matriz = [
["rojo", "amarillo", "azul"],
["amarillo", "azul", "rojo"],
["azul", "rojo", "amarillo"],
["rojo", "amarillo", "azul"],
["amarillo", "azul", "rojo"],
["azul", "rojo", "amarillo"],
];
//contenedor is a container for the instances
var contenedor:Array = new Array();
//filas holds the rows
var filas:Array = new Array();
//nombre is the name of the new instance
var nombre:String;
//Loop to get the name of the instances
for (var i:Number = 0; i < 6; i++) {
for (var j:Number = 0; j < 3; j++) {
nombre = matriz[i][j];
crearTile(nombre,32*i,32*j,i,j);
}
contenedor [i]= filas;
filas=[];
};
//function to show the instances
function crearTile(nombre:String,xPos:Number,yPos:Number,fi la: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;
}
//Function to erase all the instances
function borrarGrilla(event:MouseEvent){
for (var i:Number = 0; i < 6; i++) {
for (var j:Number = 0; j < 3; j++) {
nombre = contenedor[i][j];
removeChild(DisplayObject(getChildByName(nombre))) ;
}
}
}
Thanks in advance