Archive for the 'AS2 to AS3' Category

AS2 → AS3: Attaching Sounds From The Library

Download Example Files

AS3 no longer has an attachSound() method in the Sound object so attaching sounds from the library is a little different. It’s actually more intuitive (as usual with AS3) because it acts just like attaching bitmaps and other objects out of the library. Read more »

Tags: , , , ,

AS2 → AS3: Retrieving FlashVars

Download Example Files

I was recently working on the Image Slider that I posted here a couple of days ago and I realized I didn’t know how to access FlashVars in AS3. I quickly did a Google search and came up with the solution rather easily. Read more »

Tags: , , , ,

AS2 → AS3: Setting Masks Dynamically

Download Example Files

The more migrating I do to AS3 the more little nuances (at least nuances when I am migrating) I find. A lot of small little methods have been switched or moved into new classes and it’s always a bit of a drag to find them. Such is the case with the setMask() command. Read more »

Tags: , , , ,

AS2 → AS3: Depth Management

Download Example Files

Managing depths in AS2 was pretty cumbersome and annoying. You had to use methods like getNextHighestDepth() or keeping track of depths manually to make sure everything appeared on the stage properly and didn’t overwrite each other. Enter AS3 FTW! Read more »

Tags: , , , , ,

AS2 → AS3: Setting A MovieClip’s Color Dynamically

Download Example Files

The Color class has been deprecated since Flash 8 but was still usable in AS2 to set/get a MovieClip’s RGB values. That has since changed in AS3 and here is the new way of doing things. Read more »

Tags: , , , ,

AS2 → AS3: LoadVars AS3 Equivalent

Download Example Files

ActionScript 3 has done away with the LoadVars class and when I was updating my ContactForm for AS3 I was trying to figure out how to mimic the sendAndLoad() method that LoadVars provided. I stumbled upon an article by Peter Elst which explained how to do this so I’m going to outline the differences here. Read more »

Tags: , , , ,

AS2 → AS3: Loading & Playing External Sounds

Download Example Files

Loading and playing external sounds in AS3 is not far off from its AS2 counterpart. The code, as usual in AS3, is a little different because of the new event model, but really boils down to the same thing. Read more »

Tags: , , , , ,

AS2 → AS3: Scripted Tweening

Download Example Files
Download TweenLite

The Tween class hasn't changed at all since moving from AS2 to AS3, but there are some subtle differences in handling the events in it because of the new event architecture. Let's take a look at the code to tween a box diagonally down the stage in AS2:

Actionscript:
  1. import mx.transitions.Tween;
  2. import mx.transitions.easing.*;
  3.  
  4. var xTween:Tween = new Tween(box_mc, "_x", Regular.easeOut, 0, 500, .5, true);
  5. var yTween:Tween = new Tween(box_mc, "_y", Regular.easeOut, 0, 350, .5, true);
  6.  
  7. xTween.onMotionFinished = function():Void
  8. {
  9.     trace("Finished tweening the box.");
  10. };

Now here is that code in AS3:

Actionscript:
  1. import fl.transitions.Tween;
  2. import fl.transitions.easing.*;
  3. import fl.transitions.TweenEvent;
  4.  
  5. var xTween:Tween = new Tween(box_mc, "x", Regular.easeOut, 0, 500, .5, true);
  6. var yTween:Tween = new Tween(box_mc, "y", Regular.easeOut, 0, 350, .5, true);
  7.  
  8. xTween.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinished);
  9.  
  10. function onMotionFinished($evt:TweenEvent):void
  11. {
  12.     trace("Finished tweening the box.");
  13. }

As you can see, not much has changed, and you still have to make a new tween for each property you want to tween. Enter TweenLite, Jack Doyle's AS3 lightweight tweening engine. I now use this for AS3 tweens and it gets the job done the way I like it. It's also nice that it's just one static call. Here is the code:

Actionscript:
  1. import fl.transitions.easing.*;
  2. import gs.TweenLite;
  3.  
  4. TweenLite.to(box_mc, .5, {x: 500, y: 350, ease: Regular.easeOut, onComplete: onMotionFinished});
  5.  
  6. function onMotionFinished():void
  7. {
  8.     trace("Finished tweening the box.");
  9. }

Much shorter code and the ability to tween multiple properties in one line!

Tags: , , , ,

AS2 → AS3: Using onReleaseOutside

Download Example Files

One of the things that is weird to figure out about AS3 is why a MOUSE_UP_OUTSIDE event (or something similar) was not included. This leaves for work-arounds that mimic that behavior and this is one way I've seen it done (probably not the only way, but one of the ways to skin a cat). This is the AS2 version:

Actionscript:
  1. box_mc.onPress = function():Void
  2. {
  3.     trace("PRESSED");
  4. };
  5.  
  6. box_mc.onRelease = function():Void
  7. {
  8.     trace("RELEASED");
  9. };
  10.  
  11. box_mc.onReleaseOutside = function():Void
  12. {
  13.     trace("RELEASED OUTSIDE");
  14. };

And here is the AS3 version, not as pretty, but effective:

Actionscript:
  1. box_mc.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
  2. box_mc.buttonMode = true;
  3.  
  4. function handleMouseDown($evt:MouseEvent):void
  5. {
  6.     box_mc.alpha = .5;
  7.    
  8.     stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
  9. }
  10.  
  11. function handleMouseUp($evt:MouseEvent):void
  12. {
  13.     box_mc.alpha = 1;
  14.    
  15.     stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
  16. }

As you can see, its a bit of a hack, but it works quite well. So far I haven't had any problems using it in this manner.

Tags: , , , ,

AS2 → AS3: Running Actions On Intervals

Download Example Files

The new Timer class in AS3 gives you the ability to create and run actions at set intervals, much like the setInterval in AS2. The difference, however, is that now you can define how many times the timer runs right within the constructor. Let's take a look at the old, haggish way to do it in AS2:

Actionscript:
  1. var t:Number = setInterval(traceMessage, 1000);
  2. var count:Number = 0;
  3.  
  4. function traceMessage():Void
  5. {
  6.     if (count <10)
  7.     {
  8.         count++;
  9.        
  10.         trace("This is running once every second.");
  11.     }
  12.     else
  13.     {
  14.         clearInterval(t);
  15.        
  16.         trace("The timer has traced out ten times.");
  17.     }
  18. }

And now the new, improved, sexy way to do it in AS3:

Actionscript:
  1. var t:Timer = new Timer(1000, 10);
  2. t.addEventListener("timer", traceMessage);
  3. t.addEventListener("timerComplete", traceFinishedMessage);
  4. t.start();
  5.  
  6. function traceMessage($evt:TimerEvent):void
  7. {
  8.     trace("This is running once every second.");
  9. }
  10.  
  11. function traceFinishedMessage($evt:TimerEvent):void
  12. {
  13.     trace("The timer has traced out ten times.");
  14. }

As you can see, the Timer constructor takes in two parameters. The first is the time, in milliseconds, that each interval should run on (in this case, 1000 ms = 1 sec), and the second is the number of times that the action should repeat.

Tags: , , , ,

Next Page »