Archive for the 'My Classes' Category

AS2: AgeChecker

View Documentation
Download Class

I had a need to do some age checking on the project that I’m currently working on. I figured there had to be something out there for this already and I googled away for an age checker in Flash. I found a very handy little function written by Calvin Ly and with his permission I simply wrapped this into a class and am posting it here for anyone who may need it in the future. Thanks Calvin! Read more »

Tags: ,

AS2: SharedObjectManager

View Documentation
Download Class

A simple utility to manage shared objects and their properties.

Actionscript:
  1. import mx.events.EventDispatcher;
  2. import mx.utils.Delegate;
  3.  
  4. /**
  5. * A simple utility to manage shared objects and their properties.
  6. *
  7. * @usage
  8. * <code>
  9. * <pre>
  10. import com.reintroducing.utils.SharedObjectManager;
  11. var som:SharedObjectManager = new SharedObjectManager("userData");
  12. som.setProperty("hasVisited", "yes");
  13. trace(som.getProperty("hasVisited"));
  14. * </pre>
  15. * </code>
  16. *
  17. * @author Matt Przybylski [http://www.reintroducing.com]
  18. * @version 1.0
  19. */
  20.  
  21. class com.reintroducing.utils.SharedObjectManager
  22. {
  23. //- PRIVATE VARIABLES -------------------------------------------------------------------------------------
  24.     
  25.     private var dispatchEvent:Function;
  26.    
  27.     private var _so:SharedObject;
  28.    
  29. //- PUBLIC VARIABLES --------------------------------------------------------------------------------------
  30.    
  31.     public static var DEFAULT_NAME:String = "com.reintroducing.utils.SharedObjectManager";
  32.    
  33.     public var addEventListener:Function;
  34.     public var removeEventListener:Function;
  35.    
  36. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  37.    
  38.     /**
  39.      * Creates a new instance of the SharedObjectManager class.
  40.      *
  41.      * @usage <pre><code>var som:SharedObjectManager = new SharedObjectManager($name);</code></pre>
  42.      *
  43.      * @param $name A string value representing the shared object to create/retrieve from the user's hard drive
  44.      */
  45.    
  46.     public function SharedObjectManager($name:String)
  47.     {
  48.         EventDispatcher.initialize(this);
  49.        
  50.         this._so = SharedObject.getLocal($name);
  51.         this._so.onStatus = Delegate.create(this, onStatus);
  52.     }
  53.    
  54. //- PRIVATE METHODS ---------------------------------------------------------------------------------------
  55.    
  56.     private function onStatus($evt:Object):Void
  57.     {
  58.         switch ($evt.code)
  59.         {
  60.             case "SharedObject.Flush.Success":
  61.                 this.dispatchEvent({type: "onSOManagerSuccess", target: this});
  62.                 break;
  63.            
  64.             case "SharedObject.Flush.Failed":
  65.                 this.dispatchEvent({type: "onSOManagerFailed", target: this});
  66.                 break;
  67.         }
  68.     }
  69.    
  70. //- PUBLIC METHODS ----------------------------------------------------------------------------------------
  71.    
  72.     /**
  73.      * Sets a "cookie" (property/value pair) object in the current shared object and saves it to the user's hard drive.
  74.      *
  75.      * @usage <pre><code>som.setProperty("hasVisited", "yes");</code></pre>
  76.      *
  77.      * @param $name A string that represents the name of the property to be stored in the shared object
  78.      * @param $value A string that represents the value of the property to be stored in the shared object
  79.      *
  80.      * @return Nothing
  81.      */
  82.    
  83.     public function setProperty($name:String, $value:Object):Void
  84.     {
  85.         this._so.data[$name] = $value;
  86.         this._so.flush();
  87.     }
  88.    
  89.     /**
  90.      * Returns the value for the requested property.
  91.      *
  92.      * @usage <pre><code>som.getProperty("hasVisited");</code></pre>
  93.      *
  94.      * @param $name A string that represents the name of the property you want to retrieve
  95.      *
  96.      * @return String
  97.      */
  98.    
  99.     public function getProperty($name:String):String
  100.     {
  101.         return this._so.data[$name];
  102.     }
  103.    
  104.     /**
  105.      * Clears the current shared object.
  106.      *
  107.      * @usage <pre><code>som.clear();</code></pre>
  108.      *
  109.      * @return Nothing
  110.      */
  111.    
  112.     public function clear():Void
  113.     {
  114.         this._so.clear();
  115.     }
  116.    
  117. //- EVENT HANDLERS ----------------------------------------------------------------------------------------
  118.    
  119.    
  120.    
  121. //- GETTERS & SETTERS -------------------------------------------------------------------------------------
  122.    
  123.    
  124.    
  125. //- HELPERS -----------------------------------------------------------------------------------------------
  126.    
  127.     public function toString():String
  128.     {
  129.         return "com.reintroducing.utils.SharedObjectManager";
  130.     }
  131.    
  132. //- END CLASS ---------------------------------------------------------------------------------------------
  133. }

Tags: ,

AS2: AxisScroller v2.0

View Example
View Documentation
Download Class & Example Files

This is the latest update to the AxisScroller. This update adds a new optionalParameter, continuousScroll, which allows you to scroll (if the arrows are active) continuously by just pressing down one of the arrows. There is also a new method, reset(), that resets the scroller and content movie clips to its original positions. Lastly, some of the code was condensed to promote reuse. If you're using the AxisScroller, definitely upgrade to this version. The syntax for usage was all kept in tact so you should have no problems just replacing this in a project (nothing should be broken because of it).

Actionscript:
  1. import mx.utils.Delegate;
  2. import mx.transitions.easing.*;
  3. import mx.transitions.Tween;
  4.  
  5. /**
  6. * Creates a scroller that allows for scrolling in the x or y axis, using the mouse wheel/arrow keys, different easing functions and types, and some other miscellaneous cool things.
  7. *
  8. * @usage
  9. * <code>
  10. * <pre>
  11. import com.reintroducing.ui.AxisScroller;
  12. stop();
  13. var optionalObj:Object = new Object(
  14. {
  15.     scrollType: "easing",
  16.     isTrackClickable: true,
  17.     useArrows: true,
  18.     upArrow: content_mc.up_btn,
  19.     downArrow: content_mc.down_btn,
  20.     continuousScroll: true,
  21.     easeFunc: Regular.easeOut,
  22.     duration: .25,
  23.     arrowMove: 50,
  24.     scaleScroller: true,
  25.     autoHideControls: true
  26. });
  27. var scroller:AxisScroller = new AxisScroller(content_mc, content_mc.scroller_mc, content_mc.movie_mc, content_mc.track_mc, content_mc.mask_mc, "y", optionalObj);
  28. * </pre>
  29. * </code>
  30. *
  31. * @author Matt Przybylski [http://www.reintroducing.com]
  32. * @version 2.0
  33. */
  34.  
  35. class com.reintroducing.ui.AxisScroller
  36. {
  37. //- PRIVATE VARIABLES -------------------------------------------------------------------------------------
  38.    
  39.     private var _changeProp:String;
  40.     private var _mouseAxis:String;
  41.     private var _changeScale:String;
  42.     private var _nextPosition:Number;
  43.     private var _arrowMove:Number;
  44.     private var _scaleScroller:Boolean;
  45.     private var _autoHideControls:Boolean;
  46.     private var _mouseIsPressedDown:Boolean;
  47.     private var _trackOffset:Number;
  48.     private var _trackStart:Number;
  49.     private var _trackEnd:Number;
  50.     private var _tween:Tween;
  51.     private var _arrow1Tween:Tween;
  52.     private var _arrow2Tween:Tween;
  53.     private var _scrollerTween:Tween;
  54.     private var _trackTween:Tween;
  55.     private var _easeFunc:Function;
  56.     private var _duration:Number;
  57.     private var _holder:MovieClip;
  58.     private var _scroller:MovieClip;
  59.     private var _toBeScrolled:MovieClip;
  60.     private var _track:MovieClip;
  61.     private var _mask:MovieClip;
  62.     private var _axis:String;
  63.     private var _scrollType:String;
  64.     private var _isTrackClickable:Boolean;
  65.     private var _arrows:Boolean;
  66.     private var _upArrow:MovieClip;
  67.     private var _downArrow:MovieClip;
  68.     private var _continuousScroll:Boolean;
  69.     private var _arrowTimer:Number;
  70.     private var _arrowPressed:String;
  71.    
  72. //- PUBLIC VARIABLES --------------------------------------------------------------------------------------
  73.    
  74.     public static var DEFAULT_NAME:String = "com.reintroducing.ui.AxisScroller";
  75.  
  76. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  77.    
  78.     /**
  79.      * Creates a new instance of the AxisScroller class.
  80.      *
  81.      * <p>
  82.      * The $optionalParams parameter takes in a couple of values.
  83.      * <ul>
  84.      * <li>scrollType: A string representing the scroll type, "instant" or "easing" (default: "easing")</li>
  85.      * <li>isTrackClickable: A boolean value that makes the track clickable so that you can scroll directly to a certain spot (default: true)</li>
  86.      * <li>useArrows: A boolean value that allows the use of arrow buttons (default: false)</li>
  87.      * <li>upArrow: If useArrows, a movie clip that represents the up arrow button (default: null)</li>
  88.      * <li>downArrow: If useArrows, a movie clip that represents the down arrow button (default: null)</li>
  89.      * <li>continuousScroll: If useArrows, a boolean that specifies if when pressing the arrows the scrolling should happen continuously or not (default: false)</li>
  90.      * <li>easeFunc: A function representing the ease type you'd like to use, as per the Tween class easing functions (default: Regular.easeOut)</li>
  91.      * <li>duration: An integer (in seconds) representing the time that the scrollable content will take to move when easing is used (default: .25)</li>
  92.      * <li>arrowMove: An integer (in pixels) that the scrollable content will move onMouseWheel/Key/Arrow movement (default: 100)</li>
  93.      * <li>autoHideControls: A boolean value that hides the scroller/track/arrows (if applicable) if the scroll content is too small to scroll (default: false)</li>
  94.      * <li>scaleScroller *: A boolean value that allows automatic scaling of the scroll bar (default: false)</li>
  95.      * </ul>
  96.      * * If you use the scaleScroller optional parameter, please make the size of the scroll bar the same size as the track and this will take care of
  97.      * scaling it automatically for you.  If you AREN'T using this parameter, please size your scroll bar accordingly manually.
  98.      * </p>
  99.      *
  100.      * @usage <pre><code>var scroller:AxisScroller = new AxisScroller($holder, $scroller, $toBeScrolled, $track, $mask, $axis, $optionalParams);</code></pre>
  101.      *
  102.      * @param $holder The movie clip (or timeline) that holds all of the other clips corresponding to the scroller
  103.      * @param $scroller The movie clip to be used as the scroller
  104.      * @param $toBeScrolled The movie clip where the scrollable content resides
  105.      * @param $track The movie clip that is used as the track (measures extremes)
  106.      * @param $mask The movie clip that is used as the mask for the scrollable content area
  107.      * @param $axis A string value of either "x" or "y" that determines what axis the content is scrolled along
  108.      * @param $optionalParams Optional parameters to further control the scroller
  109.      */
  110.    
  111.     public function AxisScroller($holder:MovieClip, $scroller:MovieClip, $toBeScrolled:MovieClip, $track:MovieClip, $mask:MovieClip, $axis:String, $optionalParams:Object)
  112.     {
  113.         this._holder                = $holder;
  114.         this._scroller      = $scroller;
  115.         this._toBeScrolled     = $toBeScrolled;
  116.         this._track                     = $track;
  117.         this._mask       = $mask;
  118.         this._axis       = "_" + $axis;
  119.        
  120.         this._scrollType             = ($optionalParams.scrollType == undefined) ? "easing" : $optionalParams.scrollType;
  121.         this._isTrackClickable    = ($optionalParams.isTrackClickable == undefined) ? true : $optionalParams.isTrackClickable;
  122.         this._arrows                = ($optionalParams.useArrows == undefined) ? false : $optionalParams.useArrows;
  123.         this._upArrow           = ($optionalParams.upArrow == undefined) ? null : $optionalParams.upArrow;
  124.         this._downArrow                 = ($optionalParams.downArrow == undefined) ? null : $optionalParams.downArrow;
  125.         this._continuousScroll      = ($optionalParams.continuousScroll == undefined) ? false : $optionalParams.continuousScroll;
  126.         this._easeFunc      = ($optionalParams.easeFunc == undefined) ? Regular.easeOut : $optionalParams.easeFunc;
  127.         this._duration      = ($optionalParams.duration == undefined) ? .25 : $optionalParams.duration;
  128.         this._arrowMove                 = ($optionalParams.arrowMove == undefined) ? 100 : $optionalParams.arrowMove;
  129.         this._autoHideControls      = ($optionalParams.autoHideControls == undefined) ? false : $optionalParams.autoHideControls;
  130.         this._scaleScroller    = ($optionalParams.scaleScroller == undefined) ? false : $optionalParams.scaleScroller;
  131.        
  132.         this.manageAxis(this._axis);
  133.         this.init();
  134.     }
  135.    
  136. //- PRIVATE METHODS ---------------------------------------------------------------------------------------
  137.    
  138.     // initiates listeners and other events
  139.     private function init():Void
  140.     {
  141.         Mouse.addListener(this);
  142.         Key.addListener(this);
  143.        
  144.         this._holder.onEnterFrame       = Delegate.create(this, manageScrolling);
  145.        
  146.         this.activateButtonEvents();
  147.     }
  148.    
  149.     // re-activates the button events
  150.     private function activateButtonEvents():Void
  151.     {
  152.         this._scroller.onPress     = Delegate.create(this, manageMouseDown);
  153.        
  154.         this._scroller.onRelease = this._scroller.onReleaseOutside = Delegate.create(this, manageMouseUp);
  155.         this._scroller.useHandCursor = true;
  156.        
  157.         if (this._isTrackClickable)
  158.         {
  159.             this._track.onRelease       = Delegate.create(this, manageTrack);
  160.             this._track.useHandCursor      = true;
  161.         }
  162.        
  163.         if (this._arrows)
  164.         {
  165.             this._upArrow.onPress        = Delegate.create(this, manageUpArrow);
  166.             this._upArrow.onRelease         = Delegate.create(this, resetArrowTimer);
  167.            
  168.             this._downArrow.onPress         = Delegate.create(this, manageDownArrow);
  169.             this._downArrow.onRelease     = Delegate.create(this, resetArrowTimer);
  170.            
  171.             this._downArrow.useHandCursor = this._upArrow.useHandCursor = true;
  172.         }
  173.     }
  174.    
  175.     // destroys the events on the buttons
  176.     private function killButtonEvents():Void
  177.     {
  178.         this._scroller.onPress = this._scroller.onRelease = this._scroller.onReleaseOutside = null;
  179.         this._scroller.useHandCursor = false;
  180.        
  181.         if (this._isTrackClickable)
  182.         {
  183.             this._track.onRelease = null;
  184.             this._track.useHandCursor = false;
  185.         }
  186.        
  187.         if (this._arrows)
  188.         {
  189.             this._upArrow.onPress = this._downArrow.onPress = null;
  190.             this._upArrow.onRelease = this._downArrow.onRelease = null;
  191.             this._upArrow.useHandCursor = this._downArrow.useHandCursor = false;
  192.         }
  193.     }
  194.    
  195.     // sets the axis at which the scroller will function against
  196.     private function manageAxis($axis:String):Void
  197.     {
  198.         if ($axis == "_x")
  199.         {
  200.             this._changeProp          = "_width";
  201.             this._mouseAxis             = "_xmouse";
  202.             this._changeScale         = "_xscale";
  203.         }
  204.         else if ($axis == "_y")
  205.         {
  206.             this._changeProp          = "_height";
  207.             this._mouseAxis             = "_ymouse";
  208.             this._changeScale         = "_yscale";
  209.         }
  210.        
  211.         this._trackOffset         = - (this._scroller[this._changeProp] / 2);
  212.     }
  213.    
  214.     // calculates values and responds to changes by the scroller
  215.     private function manageScrolling():Void
  216.     {
  217.         var trackSize:Number          = this._track[this._changeProp];
  218.         var trackStart:Number       = this._track[this._axis];
  219.         var trackEnd:Number             = this._track[this._axis] + trackSize - (this._scroller[this._changeProp]);
  220.         var trackRange:Number       = trackEnd - trackStart;
  221.         var maskSize:Number             = this._mask[this._changeProp];
  222.         var movieSize:Number          = this._toBeScrolled[this