Monday, April 23, 2007

ActionScript 3 and Programming Style

I’ve began commenting with dashes to separate functions in the 60’s.  (See the ActionScript 3 code below.)  Initially I used asterisks but they required 3 holes in a punch card whereas dashes required only 1.  A card filled with 3 rows of holes would sometimes collapse in the reader.  Style has beauty and function but it also has history and structural integrity.

Before starting this blog I picked up a new book, “clear blogging” by Bob Walsh published by Apress.  I like it so far but I haven’t finished reading it.  I felt I needed some hands on experience at the same time.  However, I’m going to start exercising one of his points and that is to blog about topics I love.  One topic I love is “programming style”.

This is the ActionScript 3 code I used to measure the SWF file size in my previous post:

EFD1.as
package
{
    import flash.display.Sprite;
    import display.Header;

    public class EFD1 extends Sprite
    {
    /*  --------------------------------------------------------------------
        Constructor
        -------------------------------------------------------------------- */
           
        public function EFD1
            ()
        {
            var lvHeader:Header = new Header
            (
                200,
                [0x8A39B9, 0x9D5BBF],
                "Shop by Category"
            );
            
            addChild( lvHeader );
            
        } // Constructor
        
    } // class EFD1
    
} // package



Header.as

package display
{
    import flash.display.*;
    import flash.text.TextField;
    import flash.geom.*

    public class Header extends Sprite
    {
    /*  --------------------------------------------------------------------
        Private Static Variables
        -------------------------------------------------------------------- */
        
        // Programmers looking at my code:
        // Please don't do what I've done here.  That is, don't declare these
        // as static variables.  I did this when I was testing how much memory
        // is used.  Although this technique works and is expedient, it will
        // end up biting you later on.
        private static var svCorner     :Number = 5;
        private static var svHeight     :Number = 18.0;
        private static var svIndent     :Number = 5;
        private static var svMargin     :Number = 2;
        private static var svTextColor  :uint = 0xFFFFFF;
    
    /*  --------------------------------------------------------------------
        Private Member Variables
        -------------------------------------------------------------------- */
        
        private var mvTextField :TextField;
        
    /*  --------------------------------------------------------------------
        Constructor
        -------------------------------------------------------------------- */
    
        public function Header
            ( avWidth  :Number,
              avColors :Array,
              avText   :String = "" )
        {
            super();            
            
            var lvWidth:Number = avWidth - svMargin - svMargin;

            //Add Gradient Header           
            var lvMatrix:Matrix = new Matrix();
            lvMatrix.createGradientBox( lvWidth, svHeight, Math.PI * .5, 0, 0);
            
            graphics.beginGradientFill
            (
                GradientType.LINEAR,
                avColors,
                [1, 1],
                [0x0, 0xFF],
                lvMatrix,
                SpreadMethod.PAD
            );  
            
            graphics.drawRoundRect( svMargin, svMargin, lvWidth, svHeight, svCorner );

            // Add TextField            
            mvTextField = new TextField();
            mvTextField.x = svMargin + svIndent;
            mvTextField.y = svMargin;
            mvTextField.width = lvWidth - svIndent - svIndent;
            mvTextField.height = svHeight;
            mvTextField.textColor = svTextColor;
            mvTextField.selectable = false;
            setText( avText );
            addChild( mvTextField );
            
        } // Constructor
        
    /*  --------------------------------------------------------------------
        setText
        -------------------------------------------------------------------- */
        
        public function setText
            ( avText :String )
            :void
        {
            mvTextField.text = avText;
            
        } // setText
    
    } // class Header
    
} // package display



Code produces EFD1.swf



1 comment:

Bob Walsh said...

Thanks for the kind words Jim - and blogging is work and work takes passion to keep going.