package com.mindfock.blog {

[Learn] AS2 to AS3 Basic Liquid Layout

Posted: April 23rd, 2008 | Author: John del Rosario | Filed under: AS 3.0, Flash, Learn | Tags: , , , , , | 11 Comments »

I was just getting the hang of creating liquid layouts in AS2.0 a few months ago, and boy, is it quite a concept to grasp. Now I have to relearn everything I learned using AS3.0.Thankfully, once I understand the concept, all I have to do now is do it with unfamiliar syntax.

In this little tutorial, I’ll show AS2 and AS3 code showing the same end result. This is just very basic, just showing the differences between the two (which isn’t much), and to show how to get started with creating liquid layouts.

By the way, liquid layouts are layouts that change dynamically according the stage’s size. Not unlike liquid in a container, which takes the container’s size and shape, thus the name. ;-)

The movie shows 4 boxes I drew on four sides of the stage, changing position according to the stage’s size. Obviously, you could also adjust other properties of the MovieClip, like scale, alpha, rotation, etc.

First, the AS2.0 code:

Stage.scaleMode = “noScale”;
Stage.align = “TL”;

setStage();

var stageListener:Object = new Object();

Stage.addListener(stageListener);
stageListener.onResize = function() {
setStage();
};

function setStage() {
var WIDTH:Number = Stage.width;
var HEIGHT:Number = Stage.height;

box1._x = WIDTH-box1._width;
box1._y = HEIGHT/2-(box1._height/2);
box2._x = WIDTH/2 - (box2._width/2);
box2._y = 0;
box3._x = 0;
box3._y = box1._y;
box4._x = box2._x;
box4._y = HEIGHT - box4._height;
}

AS3.0:

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

setStage();

stage.addEventListener(Event.RESIZE, stageResize);

function setStage():void{
var WIDTH:Number = stage.stageWidth;
var HEIGHT:Number = stage.stageHeight;
box1.x = WIDTH - box1.width;
box1.y = HEIGHT/2 - (box1.height/2);
box2.x = WIDTH / 2 - (box2.width/2);
box2.y = 0;
box3.x = 0;
box3.y = box1.y;
box4.x = box2.x;
box4.y = HEIGHT - (box4.height);
}

function stageResize(e:Event):void{
setStage();
}

So, there isn’t really much difference between the two. AS3 is easier to understand because of its better event handling methods.

A little explanation for the 2 codes above, since they are quite similar. The first 2 lines are very important. Basically, they are telling the player to not do anything to objects on the stage when the stage is resized, because we want to control everything that happens when the stage is resized by code. Without those lines, the movie won’t work as expected.

The setStage() function is pretty self explanatory, it is where we do all the positioning of the movieclips. I like to put all of the code that does the positioning, resizing, etc. work in a function (preferably rolled into one), so I could call it at least once anytime I want. This is useful when initializing the movie, where all the movieclips might not be in their right place. And it is easier to put into the listener function too. You could also do it in the listener function, but I don’t recommend it.

We then call the setStage() function at least once, to position our movieclips where they should be, because when I created them in Flash, they are pretty much all over the place. If we don’t call that function, then they’ll only fall into place when we start resizing the stage.

And now to the part where the two codes are most different. Adding listeners to the stage. In AS3, it is quite straightforward. We add a listener, in this case stageResize() and listen to RESIZE events, to the stage object, which does nothing but call setStage(). You could also add other functions in there if you want to do something else when the stage resizes. In AS2, we have to create a listener object, tell that object to listen for the resizing event and execute the setStage() function. Again, you could add other functions in there.

So, there you have it. A basic Flash liquid layout. I hope this helps anyone who is having a hard time understanding this concept (I know I did). Now, this tutorial only showed changing the position of movieclips, by assigning values to them. But you could also change their position and/or size by percentages. Like 30% of the stage (I know this tutorial shows applying 50% of the stage), so you could adjust the visual elements of your movie to fit any size of resolution properly, at a limit of course.

NOTE: Notice the capital ‘S’ in Stage in the AS2 code and the small ‘s’ in the AS3 code. Don’t mess those up!

Here are the source files of this tutorial.

AS2 source (Flash 8 )

AS3 source (Flash CS3)


11 Comments on “[Learn] AS2 to AS3 Basic Liquid Layout”

  1. 1 Sisco said at 8:14 am on February 26th, 2009:

    Thanks for the nice explanation for the 2 codes above. You help me to understand it. :)

  2. 2 FF said at 5:29 pm on September 9th, 2009:

    …it was not that hard… thanks to you man! :)

  3. 3 Jupeto said at 12:31 am on October 16th, 2009:

    is it possible to create a tween effect like in this site : http://www.republicofcode.com/tutorials/flash/as3fluidresize/ using AS2?

    I’m trying this but nothing happened

    var tweenContentX:Tween = new Tween(content_mc, “x”, Bounce.easeOut, content_mc._x, (Stage.width / 2), 20, false);

    var tweenContentY:Tween = new Tween(content_mc, “y”, Bounce.easeOut, content_mc._y, (Stage.height / 2), 20, false);

  4. 4 Jupeto said at 12:45 am on October 16th, 2009:

    oops sorry, I forgot to take out

    content_mc._x = Stage.width / 2;
    content_mc._y = Stage.height / 2;

    from the function setStage() hehe, all working fine now, thank you for this AS…

    Sorry my bad… and I’ve changed the script to

    var tweenContentX:Tween = new Tween(content_mc, “_x”, Elastic.easeOut, content_mc._x, (Stage.width / 2), 2, true);

    var tweenContentY:Tween = new Tween(content_mc, “_y”, Elastic.easeOut, content_mc._y, (Stage.height / 2), 2, true);

  5. 5 Tricky said at 6:50 am on October 31st, 2009:

    I’ve been using the AS2 version, works great. However, I then tried adding an onRelease tween to box1 to move it to a new position, which worked, but then if the screen is resized again box1 obviously jumps back to the original (screen resizing) position.

    Any ideas on how to stop this happening?

    Thanks

  6. 6 John del Rosario said at 8:32 am on October 31st, 2009:

    you can place the new position in a variable, and use that variable as the target position.

  7. 7 Fluxstate said at 6:56 am on December 12th, 2009:

    Thanks for this tutorial. How would you set it so that 1 of the MCs would scale to the fit the browser window. I’m trying to get photos to scale and the navigation to stay fixed.

    Thanks!

  8. 8 John del Rosario said at 4:39 pm on December 12th, 2009:

    @Fluxstate you could try mc.width = stage.stageWidth; mc.height = stage.stageHeight;

  9. 9 Al Lemieux said at 5:24 am on March 7th, 2010:

    This code works fine in the flash player, but not when resizing the browser. I’m using Firefox and when I resize the browser nothing happens. Are there any special publish settings I should have set for this to work?

  10. 10 John del Rosario said at 7:49 am on March 7th, 2010:

    You need to set you flash to take up the whole browser window in your html. Check out SWFObject to easily embed swf’s into html.

  11. 11 ROHIT said at 7:02 pm on April 30th, 2010:

    hi…… thanks for this useful tutorial


Leave a Reply