Archive for the 'AS3' Category

Is now a good time to start using Event.ADDED_TO_STAGE vs. your class constructor for initialaztion?

I have always had a habit of doing all my init stuff in the class constructor as do most other people. But today I found a GOTCHA in Flash CS4 when publishing an AIR file.  I was trying to use a ProgressBar and set it’s value manually. I could not see the bar change it’s value even though there was no errors. Nothing I did made it work. This was a problem since there was no errors letting me know that there was a problem, the progress bar just did not work and that’s all I had to go off of.  Later I tried to place my document class code on the timeline which made the progress bar work,  leaving me even more confused and fustrated as to the problem.

In the End I found this had to do with the document class trying to set properties of the ProgressBar before the document class had a chance to be added to the stage.

Aside from my story, this may or may not ever be a problem for you , but it begs the question should this be a standered and would you follow it?

How to create and load fontSwf files and use the loaded swf’s embedded font at runtime.

I just made a video tutorial that shows you how to:

  1. Create a swf with an embedded font.
  2. Create an application and loads that swf and pulls the embedded font from the loaded swf.

This is really simple and really smart if you need to have multiple fonts in your application.

FYI: once again for best video quality use the fullscreen button.

[Reminder] Joey Lott is in ST.Louis Today at 3pm

This is just a reminder that today Joey Lott will be presenting for AS3 Apex and the Flex and Fuse the arch user group at 3pm. You can get more info on this at http://www.as3apex.com/as3/joey-lott-is-coming-to-stlouis-on-october-4th/

Hope to see you there.
- Josh Chernoff.

Innovative way to create and track loop iterators “that makes sense!”

I give credit to BigSpaceShip for this idea found at this post. Thanks your guys rock!

The thing that has always bugged the heck out of me is the naming convention for loop iterator variables.
Example: loop One would be named “i”, I always thought because it was i for iterator. What about the Second loop named “j”. Whats that stand for? and what do you name the Third loop if there is one? Well if your like me that always kinda confused you and felt convoluted.

Now here’s the cool new idea, use “i” as in Roman Numerals.
Example: loop One would be “i”, loop Two “ii”, loop Three “iii”, ect….. Now that makes sense!
Not only can you quickly identify what loop iterator your looking at but you now have a reason for name the next iterator if you need to.

I know this is not ground breaking or big news but it’s this type of thinking AS3 needs more of and I will continue to promote it so long as I see it.

Joey Lott is coming to st.Louis on October 4th

[UPDATE]::::: Meeting will now start at 3pm NOT 6pm::::: [UPDATE]

Come to a joint event with AS3 Apex and Flex and Fuse the Arch for a night with Joey Lott. There is limited space available so please RSVP by emailing me at josh@gfxcomplex.com And for you who may not know Joey Lott he has written a few O’reilly books such as Action Script Cook book and some online video tutorials about Action script over the years.

The event will be at the Drive Agency, located at 1617 Locust at 3pm on October the 4th.

Hope to see you there.
- AS3 Apex, Fuse and Flex the Arch.

Flash 10’s BitmapData.histogram is really slow?

I have been playing with BitmapData type and getPixelAt to make histograms. When I seen that the flash 10's BitmapData has a new method called histogram I thought it was a new improvement on analyzing BitmapData. Boy was I wrong. it's really really slow and I don't have clue why. Bug maybe??

Here's the code for any one who thinks it maybe that. Whats your thoughts?

FYI: with a for loop and getPixelAt I could still get 25fps at 1ms cycles. Though I have yet to see the frame rate for histogram, I cant event get close to a 1fps.

Actionscript:
  1. package
  2. {
  3.     import flash.display.BitmapData;
  4.     import flash.display.MovieClip;
  5.     import flash.display.Shape;
  6.     import flash.display.Sprite;
  7.     import flash.events.AsyncErrorEvent;
  8.     import flash.events.Event;
  9.     import flash.events.TimerEvent;
  10.     import flash.geom.Rectangle;
  11.     import flash.media.Video;
  12.     import flash.net.NetConnection;
  13.     import flash.net.NetStream;
  14.     import flash.utils.getTimer;
  15.     import flash.display.BlendMode;
  16.     import flash.utils.Timer;
  17.    
  18.     public class Main extends Sprite
  19.     {
  20.        
  21.         private var graphic1:Shape;
  22.         private var graphic2:Shape;
  23.         private var graphic3:Shape;
  24.         private var vect:Vector.<Vector.<Number>> ;     
  25.         private var bmd:BitmapData;
  26.         private var video_mc:MovieClip;
  27.        
  28.         private var rect:Rectangle;
  29.        
  30.        
  31.         public function Main():void
  32.         {
  33.             graphic1 = new Shape();
  34.             graphic2 = new Shape();
  35.             graphic3 = new Shape();
  36.            
  37.             stage.scaleMode = "noScale";
  38.             perspectiveDemo();
  39.  
  40.             bmd = new BitmapData(video_mc.width, video_mc.height);
  41.            
  42.             addChild(graphic1);
  43.             addChild(graphic2);
  44.             addChild(graphic3);
  45.            
  46.             graphic1.blendMode = BlendMode.SCREEN;
  47.             graphic1.x = 30;
  48.             graphic1.y = 200;
  49.             //graphic1.scaleY = .15;       
  50.            
  51.             graphic2.blendMode =BlendMode.SCREEN;
  52.             graphic2.x = 30;
  53.             graphic2.y = 200;
  54.             //graphic2.scaleY = .15;       
  55.            
  56.             graphic3.blendMode = BlendMode.SCREEN;
  57.             graphic3.x = 30;
  58.             graphic3.y = 200;
  59.             ///graphic3.scaleY = .15;
  60.            
  61.         }
  62.  
  63.        
  64.         private function perspectiveDemo():void
  65.         {
  66.            
  67.             video_mc = new MovieClip();
  68.             addChild(video_mc);
  69.             video_mc.x = 30;
  70.             video_mc.y = 30;
  71.            
  72.             var video:Video = new Video(320, 140);
  73.             var nc:NetConnection = new NetConnection();
  74.             nc.connect(null);
  75.             var ns:NetStream = new NetStream(nc);
  76.             ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, handleFakeError);
  77.            
  78.            
  79.             video_mc.addChild(video);
  80.             video_mc.x = 400;
  81.             video.attachNetStream(ns);
  82.             ns.play("http://gfxcomplex.com/labs/wall-e-tlr3_h320.mov");
  83.            
  84.             rect = new Rectangle(0, 0, 140, 300)
  85.            
  86.             var timer:Timer = new Timer(100);
  87.             timer.addEventListener(TimerEvent.TIMER, checkColor3);
  88.             timer.start();
  89.    
  90.         }
  91.        
  92.         private function handleFakeError(e:AsyncErrorEvent):void
  93.         {
  94.                 //
  95.         }
  96.        
  97.        
  98.         private function checkColor3(e:TimerEvent):void {
  99.  
  100.             bmd.draw(video_mc);
  101.            
  102.             vect = bmd.histogram(rect);
  103.            
  104.            
  105.             for (var i:int = 0; i <256; i++)
  106.             {         
  107.                
  108.                
  109.                 graphic1.graphics.lineStyle(.25, 0xff0000);
  110.                 graphic1.graphics.moveTo(i, 0);
  111.                 graphic1.graphics.lineTo(i, -(vect[0][i]));
  112.                
  113.                 graphic2.graphics.lineStyle(.25, 0x00ff00);
  114.                 graphic2.graphics.moveTo(i, 0);
  115.                 graphic2.graphics.lineTo(i, -(vect[1][i]));
  116.                
  117.                 graphic3.graphics.lineStyle(.25, 0x0000ff);
  118.                 graphic3.graphics.moveTo(i, 0);
  119.                 graphic3.graphics.lineTo(i, -(vect[2][i]));
  120.  
  121.             }
  122.             graphic1.graphics.clear();
  123.             graphic1.graphics.clear();
  124.             graphic1.graphics.clear();
  125.         }
  126.     }
  127. }

Having fun with runtime Color histograms and video

Here  a fun little experiment that takes screen shoots of the video at 1 millisecond cycles then breaks it down in to it's rgb channels then makes a color histogram of it.

See it here http://gfxcomplex.com/labs/histogram.html

You can download the source here. http://gfxcomplex.com/labs/histogram.zip

Great news for October in st.Louis! - Joey Lott Presenting AS3

Now things are still in the works, but I am very pleased to announce Joey Lott  "hopefully"  will be presenting for AS3 Apex in October. For any of you who are unfamiliar with Joey's work there is a not so small list of links for you at the bottom.

Now before this meeting can happen our user group needs a new location for this event per request of Joey.  So I am calling you the community to help out and donate a location for this event. If you can help please write me at josh@gfxcomplex.com or just post a reply in this blog post with a way that I can contact you.

And once again for any of you looking to learn a thing or two about Action script here is a list of books and tutorial that Joey created for you. Take my word for it, he is a great presenter and knows Action Script inside and out. If you don't believe just have a look for your self.

Online Content:
Lynda.com video Tutorials made by Joey Lott
Community MX Tutorials

Books:
ActionScript 3.0 Cookbook
Advanced ActionScript 3 with Design Patterns
Flash 8 ActionScript Bible
Programming Flex 3
Programming Flex 2
Macromedia Flash MX

Cool addChild trick! Why don’t people use the return of addChild?

I was doing some home work on the addChild method and found that it's has a return type of DisplayObject. At first I thought how strange that is since  most people would never think to use addChild as a way to get a return value.  And that's when I had an idea. Why not use the return of addChild?

A very common thing to do when you add a DisplayObject to the DisplayList is to also save that same DisplayObject in a Array so that you have fast access to that DisplayObject if you need to use removeChild or what have you.

so why not addChild and push in the same line?

Actionscript:
  1. var _mc:MovieClip = new MovieClip();
  2. var array:Array = new Array();
  3.  
  4. array.push(addChild(_mc)); // addChild with push
  5. trace(array);//outputs [object MovieClip]

I know this is really simple and not a huge deal, but since I have never seen any one else use addChild in such a way I thought it would be cool to see what every one else thinks.