Class reflection throught getDefinitionByName


/ Published in: ActionScript 3
Save to your folder(s)

Useful.


Copy this code and paste it in your HTML
  1. About Flash AS3 Class reflection ,we can use getDefinitionByName.It's flash.utils below tool class. As the function name is to get the Clash through the Name of the corresponding class objects, meaning that by the name of a class have access to the class object to the class object declared by an instance of the class object.
  2.  
  3. Note that the function of the parameters of the path + name = class class name.
  4.  
  5. For Example:
  6.  
  7. var yourclass:Class = getDefinitionByName("com.activetofocus.CustomeClass") as Class ;
  8.  
  9. var myClass:Object = new yourclass() ;
  10.  
  11. this.addChild( myClass as DisplayObject ) ;
  12.  
  13. Application:
  14.  
  15. Now use the flex to the inside design of a number of components in flash cs3, of course, you can export the swc components and then add the following libs to the project, when the need to flex some of these components inside the library to get the library as a single element , we can achieve by geDefinitionByName function, the specific implementation steps for the following:
  16.  
  17. First: In the flash cs3 in a number of components ready, and add the link name for the device (preferably the same link name and component name.) Then inside the first frame in the scene to add the following code:
  18.  
  19. function getClassByName (cname: String): Class / / cname is the name of the link components.
  20. {
  21. var mc: Class = getDefinitionByName (cname) as Class;
  22. return mc;
  23. }
  24.  
  25. Then generate swf.
  26.  
  27. Second: the swf file into the project, with a loader swf file into the program. Import successful in the loader (trigger Event.Complete events), loader.content is the swf. Then access the object getClassByName () to get the swf of the components. Into the library for later use.
  28.  
  29. Code:
  30.  
  31. var nameArr: Array = ["Candlelight", "Chrysanthemum"];
  32.  
  33. var mcArr: Array = new Array ();
  34.  
  35. function init (): void
  36.  
  37. {
  38.  
  39. var loader: Loader = new Loader ();
  40.  
  41. loader.contentLoaderInfo.addEventLinstener (Event.COMPLETE, onComplete);
  42.  
  43. loader.load (new URLRequest ("assets / firelib.swf"));
  44.  
  45. }
  46.  
  47. function onComplete (evnet: Event): void
  48.  
  49. {
  50.  
  51. for (var i: uint = 0; i
  52.  
  53. {
  54.  
  55. var cls: Class = Object (event.target.content). getClassByName (nameArr [i]); / / getClassByName () is the swf scene defined functions.
  56.  
  57. mcArr.push ({name: nameArr [i], content: new cls ()});
  58.  
  59. }
  60.  
  61. }
  62.  
  63. source: http://activetofocus.com/blog/class-reflection-throught-getdefinitionbyname-32/

URL: http://activetofocus.com/blog/class-reflection-throught-getdefinitionbyname-32/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.