Dynamic Graphics and Function.apply()

Actionscript:
  1. var cmds:Array = [["lineStyle", 0, 0xFF0000], ["drawCircle",100, 100, 50], ["drawRect", 50, 50, 100, 100]];
  2. cmds.push(["drawCircle", 100, 100, 70]);
  3. cmds.push(["beginFill",  0x555555]);
  4. cmds.push(["drawRoundRect", 80, 80, 40, 40, 10, 10]);
  5. cmds.push(["endFill"]);
  6.  
  7. render(cmds);
  8.  
  9. function render(p:Array):void {
  10.     for (var i:int = 0; i<p.length; i++) {
  11.         graphics[p[i][0]].apply(graphics,p[i].splice(1));
  12.     }
  13. }

The above creates a function called render() that takes a 2D array of Graphics class methods and then runs them. This is a very interesting technique, specifically if you'd like to write Graphics class method calls in an XML or txt file and then have them run on a given DisplayObject in flash.

I've been thinking about the best way to do this for awhile... I started off doing something very convoluted and then realized that I could use Function.apply()....

Tomorrow I'll post a snippet showing how to use this function in conjunction with XML.

This entry was posted in Graphics, dynamic, functions and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted February 5, 2009 at 2:13 am | Permalink

    That nice technique could also be used to “clone” dynamic graphics from one mc to another (to mimic AS2 duplicateMovieClip method) using proxy ( http://www.senocular.com/flash/actionscript.php?file=ActionScript_3.0/com/senocular/display/GraphicsCopy.as ).

  2. Posted February 5, 2009 at 7:48 am | Permalink

    That’s a cool idea. In a similar vein and also on senocular.com there is the new IGraphicsData class with fp10:

    http://www.senocular.com/flash/tutorials/flash10drawingapi/

    scroll down to : “Once you have a collection of IGraphicsData …”

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*