Thursday, March 22, 2012

Extended "is" function through reflection, as3

I am making a blitting class for sprite objects.
I want it to make use of objects exported for actionscript
that are embedded on the stage of .FLA file.

If there are 10 objects on the stage that are exported for
actionscript, I want the FIRST one passed to my blitter to
store it's class in a dictionary.

Then when my blitter constructor is called on the other
objects on stage, it will see that bitmap data for that class
already exists, and this new object's bitmap will be linked
to the same bitmap data.

That way, there is only ONE movie clip in memory.

To do this, I need to compare not only the baseClass
but the subClass of objects passed to the constructor.
The baseClass is used to see if the object already exists in the blitter.
The subClasses are used to see what kind of display object I am dealing with.

The class I wrote is as follows:

package com.JMIM03.UT_isType{
 import flash.utils.describeType;
 import flash.utils.getDefinitionByName;
 import flash.utils.getQualifiedClassName;
//       1         2         3         4         5         6         7         8
//345678901234567890123456789012345678901234567890123456789012345678901234567890
//#********************CODE CREDITS******************************************#//
//#                                                        |                 #//
//#         Author: John Mark Isaac Madison                |                 #//
//#         EMAIL : HeavyMetalCookies@Gmail.com            |                 #//
//#         PHONE#: (586)214-3958                          |                 #//
//#         Favorite Music: Industrial                     |                 #//
//#                                                        |                 #//
//#**************************************************************************#//
//345678901234567890123456789012345678901234567890123456789012345678901234567890
//       1         2         3         4         5         6         7         8
 
 //class summary:
 //A class that extends the "is" function
 //so that it can for example, tell that
 //an embedded asset on the stage that is exported
 //for actionscript is indeed of type "MovieClip"
 //or type "Sprite";
 
 public class UT_isType {

  public function UT_isType() {
   // empty constructor code
  }//[constructor]
  
  public function exec(
  classOrInstance:*,
  classType:Class
  ):Boolean
  {//[FN:exec]
   var coi:* = classOrInstance; //shorthand it.
   var theClass:Class;
   
   //Work with a common data type of class:
   //if input was a class, we will work on that.
   //if input was object, get it's class.
   if(coi is Class){
    theClass = coi;
   }else{
    //ATTEMPT#1:
    //first do a simple "is" check:
    if(theClass is classType){ return true; }
    
    var qn:String = getQualifiedClassName(theClass);
    theClass = Class(getDefinitionByName(qn));
   }//[make it a class]
   
   //use describeType to get XML node and parse:
   var typeString:String;
   var classAsXML:XML = describeType(theClass);
   
   //ATTEMPT#2:
   //Get the base class from it's name:
   var baseName:String = classAsXML.@name.toString();
   theClass = Class(getDefinitionByName(baseName));
   if(theClass == classType){
    return true;
   }
   
   
   //ATTEMPT#3:
   //look at extendsClass:
   var ext:XMLList = classAsXML.extendsClass;
   for each (var a:XML in ext) 
   {
    typeString = a.@type.toString();
    theClass = Class(getDefinitionByName(typeString));
    if(theClass == classType){return true;}
    
   }
   
   //ATTEMP#4:
   //look at factory node:
   var fex:XMLList = classAsXML.descendants("factory").extendsClass;
   for each (var a:XML in fex) 
   {
    typeString = a.@type.toString();
    theClass = Class(getDefinitionByName(typeString));
    if(theClass == classType){return true;}
   }
   
   //if all attempts fail, return false:
   return false;
   
  }//[FN:exec]
 }//class
}//package


//The test code:
//requires an obect on the stage with an instance id of "S2".
//that has been exported for actionscript in the library.

//S2 is an object on the stage that has
//been exported for actionscript:
import com.JMIM03.UT_isType.UT_isType;

var c2:Class = S2.constructor;
var t:Function = trace;
var T:String = "TRUE";
var F:String = "FALSE";
if(c2 is MovieClip){ t(T); }else{ t(F); }
if(c2 is Sprite)   { t(T); }else{ t(F); }
if(c2 is DisplayObjectContainer){t(T);  }
if(c2 is c2){ t("c2 is c2"); }else{t("c2 not c2");}

var isType:UT_isType = new UT_isType();
var tf:Boolean = false;
tf = isType.exec(c2,MovieClip); trace("tf==" + tf);
tf = isType.exec(c2,Sprite); trace("tf==" + tf);
tf = isType.exec(c2,DisplayObjectContainer); trace("tf==" + tf);
tf = isType.exec(c2,c2); trace("tf==" + tf);

No comments:

Post a Comment