Archive for the tag 'class'

AS3: Environment

View Example
View Documentation
Download Class & Example Files

UPDATED 10/26/07
The Environment class has been re-versioned to 2.0. It is now a singleton which means you can only have one instance of it anywhere in your application. This is a cleaner approach. The code has changed only slightly as it has added a setPaths() method and obviously you now call everything through Environment.getInstance().

UPDATED 10/22/07
Version 1.1 adds a Boolean, isLive, that can be checked to see if the site is live or not and have other parts of your code react accordingly.

A small utility class that allows you to set debug and live path variable for local and remote testing without having to toggle anything. Made in conjunction with Josh Perez.

Actionscript:
  1. /**
  2. * A small utility class that allows you to set debug and live path variable for local and remote testing without having to toggle anything.
  3. *
  4. * @usage
  5. * <code>
  6. * <pre>
  7. import com.reintroducing.debug.Environment;
  8. var env:Environment = Environment.getInstance();
  9. env.setPaths("../", "");
  10. if (env.isLive)
  11. {
  12.     trace("The site is live!");
  13. }
  14. else
  15. {
  16.     trace("The site is being debugged locally");
  17. }
  18. var xmlURL:String = env.basePath + "xml/data.xml";
  19. * </pre>
  20. * </code>
  21. *
  22. * @author Joshua Perez [http://www.joshua-studios.com]
  23. * @author Matt Przybylski [http://www.reintroducing.com]
  24. * @version 2.0
  25. */
  26.  
  27. package com.reintroducing.debug
  28. {
  29.     import flash.system.Capabilities;
  30.    
  31.     public class Environment
  32.     {
  33. //- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
  34.  
  35.         // singleton instance
  36.         private static var _instance:Environment;
  37.         private static var _allowInstance:Boolean;
  38.        
  39.         private var _debugPath:String;
  40.         private var _livePath:String;
  41.         private var _basePath:String;
  42.         private var _isLive:Boolean;
  43.        
  44. //- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
  45.        
  46.         public static const DEFAULT_NAME:String = "com.reintroducing.debug.Environment";
  47.        
  48. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  49.    
  50.         // singleton instance of Environment
  51.         public static function getInstance():Environment
  52.         {
  53.             if (Environment._instance == null)
  54.             {
  55.                 Environment._allowInstance = true;
  56.                 Environment._instance = new Environment();
  57.                 Environment._allowInstance = false;
  58.             }
  59.            
  60.             return Environment._instance;
  61.         }
  62.        
  63.         public function Environment()
  64.         {
  65.             if (!Environment._allowInstance)
  66.             {
  67.                 throw new Error("Error: Use Environment.getInstance() instead of the new keyword.");
  68.             }
  69.         }
  70.        
  71. //- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
  72.        
  73.         private function setProperPath():void
  74.         {
  75.             if (Capabilities.playerType == "PlugIn" || Capabilities.playerType == "ActiveX")
  76.             {
  77.                 this._basePath = this._livePath;
  78.                 this._isLive = true;
  79.             }
  80.             else
  81.             {
  82.                 this._basePath = this._debugPath;
  83.                 this._isLive = false;
  84.             }
  85.         }
  86.        
  87. //- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
  88.    
  89.         /**
  90.          * Sets the proper debug/live paths.
  91.          *
  92.          * @usage <pre><code>Environment.getInstance().setPaths("../", "");</code></pre>
  93.          *
  94.          * @param $debugPath A string representing the local (debug) path
  95.          * @param $livePath A string representing the remote (live) path
  96.          */
  97.        
  98.         public function setPaths($debugPath:String, $livePath:String):void
  99.         {
  100.             this._debugPath = $debugPath;
  101.             this._livePath = $livePath;
  102.            
  103.             this.setProperPath();
  104.         }
  105.    
  106. //- EVENT HANDLERS ----------------------------------------------------------------------------------------
  107.    
  108.        
  109.    
  110. //- GETTERS & SETTERS -------------------------------------------------------------------------------------
  111.    
  112.         /**
  113.          * Returns the basePath for use in loading assets.
  114.          *
  115.          * @usage <pre><code>var xmlURL:String = Environment.getInstance().basePath + "xml/data.xml";</code></pre>
  116.          *
  117.          * @return String
  118.          */
  119.        
  120.         public function get basePath():String
  121.         {
  122.             return this._basePath;
  123.         }
  124.        
  125.         /**
  126.          * Returns a Boolean value if the site is on a live server or being tested locally.
  127.          *
  128.          * @usage <pre><code>var siteLive:Boolean = Environment.getInstance().isLive;</code></pre>
  129.          *
  130.          * @return Boolean
  131.          */
  132.        
  133.         public function get isLive():Boolean
  134.         {
  135.             return this._isLive;
  136.         }
  137.    
  138. //- HELPERS -----------------------------------------------------------------------------------------------
  139.    
  140.         public function toString():String
  141.         {
  142.             return "com.reintroducing.debug.Environment";
  143.         }
  144.    
  145. //- END CLASS ---------------------------------------------------------------------------------------------
  146.     }
  147. }

Tags: , , , ,

AS2: MousePositionPanel

View Example
View Documentation
Download Class & Example Files

Creates a scrolling panel that allows for scrolling in the x or y axis depending on where the mouse pointer is within the panel.

Actionscript:
  1. /**
  2. * Creates a scrolling panel that allows for scrolling in the x or y axis depending on where the mouse pointer is within the panel.
  3. *
  4. * @usage
  5. * <code>
  6. * <pre>
  7. import com.reintroducing.ui.MousePositionPanel;
  8. var mpp:MousePositionPanel = new MousePositionPanel(holder_mc, holder_mc.content_mc, holder_mc.mask_mc, "x", "withMouse", 10);
  9. holder_mc.mask_mc.onRollOver = function():Void
  10. {
  11.     mpp.startPanel();
  12. };
  13. holder_mc.mask_mc.onRollOut = function():Void
  14. {
  15.     mpp.stopPanel();
  16. };
  17. * </pre>
  18. * </code>
  19. *
  20. * @author Matt Przybylski [http://www.reintroducing.com]
  21. * @version 1.0
  22. */
  23.  
  24. class com.reintroducing.ui.MousePositionPanel
  25. {
  26. //- PRIVATE VARIABLES -------------------------------------------------------------------------------------
  27.    
  28.     private var _holder:MovieClip;
  29.     private var _mc:MovieClip;
  30.     private var _mask:MovieClip;
  31.     private var _axis:String;
  32.     private var _moveType:String;
  33.     private var _moveDir:Number;
  34.     private var _mouseAxis:String;
  35.     private var _changeProp:String;
  36.     private var _speed:Number;
  37.     private var _end:Number;
  38.     
  39. //- PUBLIC VARIABLES --------------------------------------------------------------------------------------
  40.    
  41.     public static var DEFAULT_NAME:String = "com.reintroducing.ui.MousePositionPanel";
  42.    
  43. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  44.    
  45.     /**
  46.      * Creates a new instance of the MousePositionPanel class.
  47.      *
  48.      * @usage <pre><code>var mpp:MousePositionPanel = new MousePositionPanel($holder, $mc, $mask, $axis, $moveType, $speed);</code></pre>
  49.      *
  50.      * @param $holder The movie clip (or timeline) that holds the content and mask clips
  51.      * @param $mc The movie clip to be scrolled
  52.      * @param $mask The movie clip that is used as the mask for the scrollable content area
  53.      * @param $axis A string value of either "x" or "y" that determines what axis the content is scrolled along
  54.      * @param $moveType A string, "withMouse" (moves with the mouse) or "againstMouse" (moves away from the mouse)
  55.      * @param $speed A number that represents how fast to scroll the clip
  56.      */
  57.    
  58.     public function MousePositionPanel($holder:MovieClip, $mc:MovieClip, $mask:MovieClip, $axis:String, $moveType:String, $speed:Number)
  59.     {
  60.         this._holder                    = $holder;
  61.         this._mc                        = $mc;
  62.         this._mask            = $mask;
  63.         this._axis       = "_" + $axis;
  64.         this._moveType          = $moveType;
  65.         this._speed      = $speed;
  66.        
  67.         this.manageAxis(this._axis);
  68.         this.manageMoveType();
  69.     }
  70.    
  71. //- PRIVATE METHODS ---------------------------------------------------------------------------------------
  72.    
  73.     private function manageAxis($axis:String):Void
  74.     {
  75.         if ($axis == "_x")
  76.         {
  77.             this._changeProp          = "_width";
  78.             this._mouseAxis             = "_xmouse";
  79.         }
  80.         else if ($axis == "_y")
  81.         {
  82.             this._changeProp          = "_height";
  83.             this._mouseAxis             = "_ymouse";
  84.         }
  85.     }
  86.    
  87.     private function manageMoveType():Void
  88.     {
  89.         if (this._moveType == "withMouse")
  90.         {
  91.             this._moveDir = 1;
  92.         }
  93.         else if (this._moveType == "againstMouse")
  94.         {
  95.             this._moveDir = -1;
  96.         }
  97.         else
  98.         {
  99.             this._moveDir = 1;
  100.         }
  101.     }
  102.    
  103.     private function movePanel(dir:Number):Void
  104.     {
  105.         var owner:MousePositionPanel = this;
  106.         var mousePercent:Number = (this._holder[this._mouseAxis] / this._mask[this._changeProp]);
  107.         var mSpeed:Number;
  108.        
  109.         if (dir == 1)
  110.         {
  111.             mSpeed = 1 - mousePercent;
  112.         }
  113.         else
  114.         {
  115.             mSpeed = mousePercent;
  116.         }
  117.        
  118.         this._end = Math.round(-((this._mc[this._changeProp] - this._mask[this._changeProp]) * mSpeed));
  119.        
  120.         this._mc.onEnterFrame = function():Void
  121.         {
  122.             if (this[owner._axis] == owner._end)
  123.             {
  124.                 delete this.onEnterFrame;
  125.             }
  126.             else if (this[owner._axis]> owner._end)
  127.             {
  128.                 this[owner._axis] -= Math.ceil((this[owner._axis] - owner._end) * (owner._speed / 100));
  129.             }
  130.             else if (this[owner._axis] <owner._end)
  131.             {
  132.                 this[owner._axis] += Math.ceil((owner._end - this[owner._axis]) * (owner._speed / 100));
  133.             }
  134.         };
  135.     }
  136.    
  137. //- PUBLIC METHODS ----------------------------------------------------------------------------------------
  138.    
  139.     /**
  140.      * Starts the panel scrolling in the proper direction.
  141.      *
  142.      * @usage <pre><code>mpp.startPanel();</code></pre>
  143.      *
  144.      * @return Nothing
  145.      */
  146.    
  147.     public function startPanel():Void
  148.     {
  149.         var owner:MousePositionPanel = this;
  150.        
  151.         _level0.onMouseMove = function():Void
  152.         {
  153.             owner.movePanel(owner._moveDir);
  154.         };
  155.     }
  156.    
  157.     /**
  158.      * Stops the panel from scrolling any further.
  159.      *
  160.      * @usage <pre><code>mpp.stopPanel();</code></pre>
  161.      *
  162.      * @return Nothing
  163.      */
  164.    
  165.     public function stopPanel():Void
  166.     {
  167.         _level0.onMouseMove = null;
  168.     }
  169.    
  170. //- EVENT HANDLERS ----------------------------------------------------------------------------------------
  171.    
  172.    
  173.    
  174. //- GETTERS & SETTERS -------------------------------------------------------------------------------------
  175.    
  176.    
  177.    
  178. //- HELPERS -----------------------------------------------------------------------------------------------
  179.    
  180.     public function toString():String
  181.     {
  182.         return "com.reintroducing.ui.MousePositionPanel";
  183.     }
  184.    
  185. //- END CLASS ---------------------------------------------------------------------------------------------
  186. }

Tags: , , ,

AS2: ContactForm

View Example
View Documentation
Download Class & Example Files

Creates a communication gateway between a contact form and a PHP script that checks for validity and sends the email. The e-mail checking is rudimentary at best, but it gets the job done. If someone will go through the trouble to try and break this e-mail checker, then by all means spam away (at least that's the way I see it).

Actionscript:
  1. import mx.events.EventDispatcher;
  2.  
  3. /**
  4. * Creates a communication gateway between a contact form and a PHP script that checks for validity and sends the email.  The
  5. * e-mail checking is rudimentary at best, but it gets the job done.  If someone will go through the trouble to try and break this
  6. * e-mail checker, then by all means spam away (at least that's the way I see it).
  7. *
  8. * <p>The fields array should contain an object for each field.  The object will have a "field" string value, which is the
  9. * instance name (minus the "_txt" suffix) of the text field to check, an "error" string value which is the error text
  10. * that is displayed when the error events are dispatched, and a "required" boolean that checks if the field is a
  11. * required field.</p>
  12. *
  13. * <p>As of right now, the fields that are passed into the fields array should be written as the instance name of the field minus the _txt extension.
  14. * For example, if the instance name is name_txt, you pass "name" as the field string value in the fields array.</p>
  15. *
  16. * <p>The class dispatches events depending on what happens.  The events are as follows:</p>
  17. *
  18. * <ul>
  19. * <li>onEmailError: Dispatched if the e-mail check returns an incorrect e-mail address</li>
  20. * <li>onInvalidError: Dispatched when a required field fails the check (displays error from fields array object)</li>
  21. * <li>onMessageSent: Dispatched when the message has been successfully sent</li>
  22. * <li>onServerError: Dispatched if the server is unable to deliver the message</li>
  23. * </ul>
  24. *
  25. * @usage
  26. * <code>
  27. * <pre>
  28. import com.reintroducing.forms.ContactForm;
  29. var listener:Object = new Object();
  30. var scriptURL:String = "scripts/contactSubmit.php";
  31. var toEmail:String = "matt@reintroducing.com";
  32. var subject:String = "Testing the ContactForm Class";
  33. var fields:Array = new Array(   {field: "name", error: "Please enter your name.", required: true},
  34.                                 {field: "email", error: "Please enter a valid e-mail address.", required: true},
  35.                                 {field: "phone", error: "", required: false},
  36.                                 {field: "comments", error: "Please enter your comments.", required: true});
  37. var cf:ContactForm = new ContactForm(fields, scriptURL, toEmail, subject);
  38. cf.addEventListener("onEmailError", listener);
  39. cf.addEventListener("onInvalidError", listener);
  40. cf.addEventListener("onMessageSent", listener);
  41. cf.addEventListener("onServerError", listener);
  42. listener.onEmailError = function($eo:Object):Void
  43. {
  44.     _root.error_txt.text = $eo.error;
  45. };
  46. listener.onInvalidError = function($eo:Object):Void