Archive for the 'AS2' 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: ,

SharedObjectManager v1.1 Updates

Reader Stéphane e-mailed me last week with a problem he was having using the SharedObject in Flash. Basically, Stéphane wanted to save some data in one SWF file to a SharedObject and then retrieve that data in another SWF file. I had done this before many times but quickly realized that I had only done this with the same SWF file later loading that data back in. I did a little thinking about it and I quickly realized the error of my ways. Read more »

Tags: , , ,

Getting Started With Scripted Tweening

Download Example Files

As someone who is a beginner starting out with developing in Flash, it’s not always easy to figure out how to tween things through code and what products to use. There are more than enough tweening engines out there to make your head spin and some are more well known than others. Here I hope to shed a bit of light on what tweening engines can do what and are best used in what situation. Read more »

Tags: , , , ,

AS2: Animating Filters With Fuse

View Example
Download Fuse
Download Example Files

I still use Fuse for my scripted animations in AS2 so I figured I'd show how I animate filters using Fuse in AS2 projects. In this instance, I used the glow filter to animate when you roll over a button. The filter animation in Fuse is pretty much the same no matter what filter you use (of course you have to change the properties of the filter to tween) and you can see all the properties of each tweenable filter in the docs. This is comprised of a class file I made for the button and just some quick code on the timeline to show how to use it.

Here is GlowButton.as:

Actionscript:
  1. import mx.events.EventDispatcher;
  2. import mx.utils.Delegate;
  3. import com.mosesSupposes.fuse.Fuse;
  4. import com.mosesSupposes.fuse.FuseFMP;
  5. import com.mosesSupposes.fuse.PennerEasing;
  6.  
  7. /**
  8. * @author Matt Przybylski [http://www.reintroducing.com]
  9. * @version 1.0
  10. */
  11.  
  12. class com.reintroducing.ui.GlowButton extends MovieClip
  13. {
  14. //- PRIVATE VARIABLES -------------------------------------------------------------------------------------
  15.     
  16.     private var dispatchEvent:Function;
  17.    
  18.     private var _fuse:Fuse;
  19.     private var _id:Number;
  20.     private var _startBlur:Number;
  21.     private var _endBlur:Number;
  22.     private var _active:Boolean;
  23.    
  24. //- PUBLIC VARIABLES --------------------------------------------------------------------------------------
  25.    
  26.     public static var DEFAULT_NAME:String = "com.reintroducing.ui.GlowButton";
  27.    
  28.     public var addEventListener:Function;
  29.     public var removeEventListener:Function;
  30.    
  31.     // instances
  32.     public var title_txt:TextField;
  33.    
  34. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  35.    
  36.     public function GlowButton()
  37.     {
  38.         super();
  39.        
  40.         EventDispatcher.initialize(this);
  41.        
  42.         this.onLoad = Delegate.create(this, init);
  43.     }
  44.    
  45. //- PRIVATE METHODS ---------------------------------------------------------------------------------------
  46.    
  47.     private function init():Void
  48.     {
  49.         this.initButtonEvents();
  50.     }
  51.    
  52.     /**
  53.      * Adds the events to the button
  54.      */
  55.     private function initButtonEvents():Void
  56.     {
  57.         this.onRollOver = Delegate.create(this, doRollOver);
  58.         this.onRollOut  = Delegate.create(this, doRollOut);
  59.         this.onRelease  = Delegate.create(this, doRelease);
  60.     }
  61.    
  62.     /**
  63.      * Clears the button of events
  64.      */
  65.     private function killButtonEvents():Void
  66.     {
  67.         this.onRollOver = null;
  68.         this.onRollOut  = null;
  69.         this.onRelease  = null;
  70.     }
  71.    
  72.     /**
  73.      * onRollOver
  74.      */
  75.     private function doRollOver():Void
  76.     {
  77.         this._fuse = new Fuse({target: this, Glow_blur: this._endBlur, Glow_alpha: 1, Glow_strength: 2, time: .35, ease: PennerEasing.easeOutSine});
  78.         this._fuse.start();
  79.     }
  80.    
  81.     /**
  82.      * onRollOut
  83.      */
  84.     private function doRollOut():Void
  85.     {
  86.         this._fuse = new Fuse({target: this, Glow_blur: this._startBlur, Glow_alpha: .5, Glow_strength: 2, time: .35, ease: PennerEasing.easeOutSine});
  87.         this._fuse.start();
  88.     }
  89.    
  90.     /**
  91.      * onRelease
  92.      */
  93.     private function doRelease():Void
  94.     {
  95.         this.dispatchEvent({type: "onGlowButtonClicked", target: this});
  96.     }
  97.    
  98. //- PUBLIC METHODS ----------------------------------------------------------------------------------------
  99.    
  100.     /**
  101.      * Sets the properties of the button
  102.      */
  103.     public function create($title:String, $id:Number, $color:Number, $startBlur:Number, $endBlur:Number):Void
  104.     {
  105.         this.title_txt.text = $title;
  106.         this._id = $id;
  107.         this._startBlur = $startBlur;
  108.         this._endBlur = $endBlur;
  109.        
  110.         FuseFMP.writeFilter(this, "Glow", {color: $color, blur: this._startBlur, alpha: .5, strength: 2});
  111.     }
  112.    
  113.     /**
  114.      * Enables the button
  115.      */
  116.     public function setEnabled():Void
  117.     {
  118.         this.initButtonEvents();
  119.        
  120.         this.enabled = true;
  121.         this._active = true;
  122.        
  123.         this._fuse = new Fuse({target: this, alpha: 100, Glow_blur: this._startBlur, Glow_alpha: .5, Glow_strength: 2, time: .35, ease: PennerEasing.easeOutSine});
  124.         this._fuse.start();
  125.     }
  126.    
  127.     /**
  128.      * Disables the button
  129.      */
  130.     public function setDisabled():Void
  131.     {
  132.         this.killButtonEvents();
  133.        
  134.         this.enabled = false;
  135.         this._active = false;
  136.        
  137.         this._fuse = new Fuse({target: this, alpha: 50, Glow_blur: 0, Glow_alpha: 0, Glow_strength: 0, time: .35, ease: PennerEasing.easeOutSine});
  138.         this._fuse.start();
  139.     }
  140.    
  141. //- EVENT HANDLERS ----------------------------------------------------------------------------------------
  142.    
  143.    
  144.    
  145. //- GETTERS & SETTERS -------------------------------------------------------------------------------------
  146.    
  147.     public function get id():Number
  148.     {
  149.         return this._id;
  150.     }
  151.    
  152.     public function set id($val:Number):Void
  153.     {
  154.         this._id = $val;
  155.     }
  156.    
  157.     public function get active():Boolean
  158.     {
  159.         return this._active;
  160.     }
  161.    
  162.     public function set active($val:Boolean):Void
  163.     {
  164.         this._active = $val;
  165.     }
  166.    
  167. //- HELPERS -----------------------------------------------------------------------------------------------
  168.    
  169.     public function toString():String
  170.     {
  171.         return "com.reintroducing.ui.GlowButton";
  172.     }
  173.    
  174. //- END CLASS ---------------------------------------------------------------------------------------------
  175. }

And here is the code on the timeline which just sets up the buttons:

Actionscript:
  1. import com.reintroducing.ui.GlowButton;
  2. import com.mosesSupposes.fuse.*;
  3.  
  4. ZigoEngine.register(Fuse, FuseFMP, PennerEasing);
  5.  
  6. var numButtons:Number = 4;
  7.  
  8. function setButtons():Void
  9. {
  10.     var btn:GlowButton;
  11.    
  12.     for (var i:Number = 0; i <numButtons; i++)
  13.     {
  14.         btn = GlowButton(this["btn" + i + "_mc"]);
  15.         btn.create("button" + i, i, 0x996600, 5, 10);
  16.         btn.addEventListener("onGlowButtonClicked", this);
  17.     }
  18. }
  19.  
  20. function onGlowButtonClicked($evt:Object):Void
  21. {
  22.     for (var i:Number = 0; i <numButtons; i++)
  23.     {
  24.         var btn:GlowButton = GlowButton(this["btn" + i + "_mc"]);
  25.        
  26.         if (btn.active == false)
  27.         {
  28.            
  29.             btn.setEnabled();
  30.         }
  31.     }
  32.    
  33.     $evt.target.setDisabled();
  34. }
  35.  
  36. setButtons();

I'm quite aware this isn't the prettiest example (as my examples usually are on the ugly side), but it's the code that counts here :P

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: Record & Save Audio To Server With Flash Media Server

Download Example Files

The other day I posted a tip on how to set up Flash Media Server Developer Edition locally on your PC. To reiterate, I'm by no means an expert with Flash Media Server and this was the only test I had done with it, but I figured I'd show how to record some audio using your microphone and FMS and save it on to the FMS server as an .flv file. Let's take a look at the code:

Actionscript:
  1. stop();
  2.