ThinkGeek - Cool Stuff for Geeks and Technophiles

December 29, 2003

Flexibility matters (AS2)

Yes, you guessed it, we can do something quite similar with the brand new AS 2.0

First of all, we’ll write a new class:

class net.designnation.exp.Dinamica{
 public var prop1:String;
 public var prop2:String;
 public var prop3:String;
 
 function Dinamica(){
  
 }
 	
 public function metodo(Void):Void{
  	trace("method ------");
  	trace(prop1 + " " + prop2 + " " + prop3);
 }
}

In the first frame of the main timeline, paste the following code:

import net.designnation.exp.*;

var testClass:Dinamica = new Dinamica();

testClass.prop1 = "Hi!";
testClass["prop2"] = "I’m";
var cadena:String = "prop3";
testClass[cadena] = "testing";

//To retrieve tose values:
for (var k=1;k<4;k++){
 	trace(testClass["prop"+k]);
}

//To execute the class method

testClass.metodo();
//or
testClass["metodo"]();
//or 
var cadena2:String="metodo";
testClass[cadena2]();

stop();

So, this is the way (at least one of the ways) to make one class method call another without even knowing it’s name, or to make one class call another one’s methods, ….

Anyway, this shows the power and flexibility of ActionScript.

Posted by Cesar Tardaguila Date: December 29, 2003 09:25 PM
Comments