THEACTIONSCRIPTER.COM
THEACTIONSCRIPTER.COM

Shared Object / Flash Cookie in ActionScript 3

Sometimes you may want to store information on the clients machine.  You can do this using a Flash Shared Object, this is basically a Flash Cookie.  A Flash Cookie is actually a little better than a normal cookie.  Flash Cookies are a little more secure and cannot be deleted like your browser's normal cookies are.  Very few users know how to delete them, which is pretty cool for us Flash Developers.

Here is how you set a SharedObject in ActionScript 3:

var __so:SharedObject = SharedObject.getLocal("theactionscripter");
__so.data.favoriteWebsite = "http://www.theactionscripter.com";
__so.data.userId = "123";
__so.flush(); //Immediately writes a locally persistent shared object to a local file.
 

Here is how you get a SharedObject in ActionScript 3:
var __so:SharedObject = SharedObject.getLocal("theactionscripter");
trace(__so.data.favoriteWebsite ); //http://www.theactionscripter.com
  
Here is how you clear a SharedObject in ActionScript 3:
var __so:SharedObject = SharedObject.getLocal("theactionscripter");
__so.clear();
 

View Demo of SharedObject.



Source Code

gSkinner Made Me Lazy

gSkinner came out with this little framework called TweenMax to help ActionScripters with tweening animation.  Let me say, I am a huge fan of it.  I use it on almost all of my projects.  It makes my life very easy and I can write code a lot faster.  Here is the issue, I've been using it so much that if I had to create a tween without it, I wouldn't be able to do it off the top of my head like other code.

Skinner has an AS2 and AS3 version to make programming easier.  TweenMax does it all for you. 

  • x/y axis
  • alpha
  • eases
  • onComplete functionality
  • onComplete functionality w/ params
  • rotation
  • tint
  • motionBlur
  • glow
  • blurFilter
  • bevelFilter
  • Papervision Items
  • and many more

What is so special about it, is how easy it is to learn and use.  When you download the TweenMax framework you will receive a swf that you can demo any of the animations and at the bottom of the demo the code will be written for you.  What could be easier than that?  Nothing.  This is why I'm becoming lazy. 

import gs.TweenMax;
TweenMax.to(mc, 1, {x:46, y:43});
If you need to tween multiple items all you have to do is set up a tween for each item, add them to an array and set them to the TweenGroup class.  You can even squence your tweens in any order, add delays between each of them and more.

TweenGroup(tweens:Array, DefaultTweenClass:Class, align:String, stagger:Number)
Or if you want to apply the same tween to the a number of items you can do the following:

TweenGroup.allTo([mc1, mc2, mc3, mc4, mc5], 1, {y:"100", alpha:0, stagger:0.2});
TweenMax is a great framework and worth taking a look at.  It makes coding a lot faster and I haven't had any performance issue I could blame on the framework.  It is worth your time to check it out (http://blog.greensock.com).

Accessing the root/stage in ActionScript 3

Wait you can't access the root anymore in AS3.  It is gone.  You have to use stage instead of _root.  If you are in your document class you can access the stage by stage.  You can also access the stage from any other movieClip.

mcWhatever.stage

Preloading in AS3 with Linked Items in your Library

In AS2 you could add all your library's linked items onto a frame that was never getting called to make sure they would preload correctly.  You can no longer do this in AS3.  In AS3 your linked items will still preload in frame 0 before your preloading code will be executed.  I found two work a rounds in ActionScript 3.  One way to do it is explained good at http://www.bit-101.com/blog/?p=946.  This way is a little tricky, especially for beginners.  You will have to use the [Frame] metadata tag.

The way I found that is easiest and fastest is to just have a blank swf that handles the preloading of your other swf.  This way you make sure your preloader will show up before frame 0.

Put the following code into the first frame of the swf that is handling the preloading.

stop();

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
l.load(new URLRequest("mySWF.swf"));

function loop(e: progressEvent):void{ 
    var perc:Number = Math.round(e.bytesLoaded*100 / e.bytesTotal); 
    mcLoader.txtPercent.text = perc + "%";
}

function done(e:Event):void{ 
    l.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loop);
    l.contentLoaderInfo.removeEventListener(Event.COMPLETE, done); 
    removeChild(mcLoader); 
    addChild(l);
}
 
The only thing to be aware of is how to access you FlashVars and that the swf for the preloading is now your stage.


Source Code

Open URL in ActionScript 3

No more getUrl() in ActionScript 3.  AS3 makes you type another line or two, but it is not big deal.   

var _url:URLRequest = new URLRequest("http://www.theactionscripter.com/");
navigateToURL(targetURL);

If you want to open your url into an new window you will add "_blank" to the navigateToURL call.

var _url:URLRequest = new URLRequest("http://www.theactionscripter.com/");
navigateToURL(targetURL, "_blank");

Cool Papervision Websites : Part One

There is some really cool stuff being done with Papervision right now on the web.  Take a look of some of my favorite Papervision websites.


Aerial Virtual Tour of New York

  This is a cool Papervision version of Google Maps.  You are able to look at New York at angles and zooms. 

http://www.pixelcase.com.au/vr/2009/newyork/                        


Air Jordan 2009

   Jordan was a master on the court and on the web.

http://www.nike.com/jumpman23/aj2009/                        

AS3 : stage.width vs stage.stageWidth

A number of times I have had to look up what the difference is between stage.width and stage.stageWidth in ActionScript 3.  They can easily be confused because they are very similar.  Basically stage.width is the total width for all the items on your stage and stage.stageWidth is the width of the swf canvas.

According to Adobe Docs

stage.width
Indicates the width of the display object, in pixels. The width is calculated based on the bounds of the content of the display object.

stage.stageWidth
Specifies the current width, in pixels, of the Stage.

When the value of the scaleMode property is set to StageScaleMode.NO_SCALE, the stageWidth property represents the width of Flash Player. This means that the stageWidth property varies as you resize the Flash Player window. When the value of the scaleMode property is not set to StageScaleMode.NO_SCALE, the stageWidth property represents the width of the SWF file as set during authoring in the Document Properties dialog box. This means that the value of the stageWidth property stays constant as you resize the Flash Player window. This property cannot be set.

stage.width vs stage.stageWidth Demo

In the example below you can dynamically adjust the width of the box to see the difference between stage.width and stage.stageWidth.  The stage.width will never be smaller than 234 because of the size of the toolbox at the bottom.

AS 3 Events Imports

In actionscript 3 you must define all your imports correctly or you will receive errors when you compile.  If you are new to AS3, imports are like your headers.  It is always a pain for me to look up the correct import.  So I decided to make a list of all the full imports path for the events. 

You could always take the short cut and do import flash.events.*;  I am not a big fan of this.  I think this is coincided lazy programming.  It is good practice to put the full extension for each import package. 

Events Imports List

  • import flash.events.ActivityEvent
  • import flash.events.AsyncErrorEvent
  • import flash.events.ContextMenuEvent
  • import flash.events.DataEvent
  • import flash.events.ErrorEvent
  • import flash.events.Event
  • import flash.events.EventDispatcher
  • import flash.events.EventPhase
  • import flash.events.FocusEvent
  • import flash.events.FullScreenEvent
  • import flash.events.HTTPStatusEvent
  • import flash.events.IMEEvent
  • import flash.events.IOErrorEvent
  • import flash.events.KeyboardEvent
  • import flash.events.MouseEvent
  • import flash.events.NetStatusEvent
  • import flash.events.ProgressEvent
  • import flash.events.SecurityErrorEvent
  • import flash.events.StatusEvent
  • import flash.events.SyncEvent
  • import flash.events.TextEvent
  • import flash.events.TimerEvent

Error 1180 addframescript

Error 1180: addframescript is usually related to the Document class.  You should first review how you reference your document class. Go to publish settings > flash tab > click on the settings button next to the ActionScript version > check to make sure the Classpath is correct.

I received this error because I had two different pathing to my Document class because I had multiple versions of the fla.  Even though the second pathing was correct it was reading the first one and getting confused.  To be safe, delete all paths and add them again.

Also, make sure your document class is extending MovieClip.  Your Document class is probably trying to access a function like stop() which is a property of the MovieClip class.

Let me know.

AS3 attachMovie() from Library

AS2 fans, you will no longer be using attachMovie() to attach items from the library to the stage.  ActionScript 3 removed the attachMovie() function.  Now you have to write 2 lines of code to attach an item from the library.  It is pretty simple.

AS3 code example - Attaching Item from Library

var item:item_one_mc = new item_one_mc();        //first declare library item
this.addChild(item);                                                      //add child to the stage

unloadMovie() has also been removed in AS3.  Instead of using unloadMovie(), you need to use removeChild().

AS3 code example - Removing Item from Stage

this.removeChild(item); 

The example below uses AS3 to attach an item from the library then remove it.



download as3_attachMovie_example

Sponsored Links

Recent Posts

  1. HTML5 vs Flash
    Sunday, June 13, 2010
  2. Free Gucci Mane Flash Game
    Monday, January 18, 2010
  3. Flash Input Text Font Issue with Mask
    Tuesday, January 05, 2010
  4. Developing Flash on a Mac vs PC
    Tuesday, December 29, 2009
  5. AS 3 Hide/Show Mouse Cursor
    Tuesday, December 15, 2009
  6. Where Can I Find Omniture Flash ActionSource Documentation?
    Friday, November 06, 2009
  7. Flash Making Extra Omniture Tracking Calls
    Thursday, November 05, 2009
  8. Connecting to a Web Service in Flex
    Wednesday, November 04, 2009
  9. Reading a RSS/Twitter feed with ActionScript 3
    Tuesday, November 03, 2009
  10. AS3 :: Get all Children MovieClips
    Friday, October 09, 2009

My Bukisa Articles


Subscribe


Blog Software