Flash Liquid Layout Dynamic Positioning

Following another post regarding using stage listeners to dynamically resize and position MovieClips in Flash, I was asked to upload and FLA as an example. The previous code can just be pasted into the actions panel and will run without any MovieClips needed, and was designed to show the StageListener function rather than showcase a particular dynamic layout.

Download: Fluid Flash Layout Example

Anyway, here is a slightly amended version of this simple technique which uses MovieClips, and produces a fluid layout based on percentages (much like the classic CSS three column layout).

Again, this can be easily modified to include more elements, margins etc. Note – if you want to add content to the Movieclips (for example A menu in the left column), simply make the resizable elements a background MovieClip inside each parent MovieClip.

For instance, our left column would then be resized by using:

leftCol.backGround.pos(x, y, w, h);

The content inside the leftCol MovieClip would then maintain its size as the background resizes. Easy.

// Setup the Stage properties
Stage.scaleMode = "noScale";
Stage.align = "TL";

// Variables (in pixels)
var headHeight:Number = 100;
var footHeight:Number = 50;

// Variables (in percent)
var leftColWidth:Number = 25;
var mainColWidth:Number = 60;
var rightColWidth:Number = 15;

// Prototype function to position MovieClips
MovieClip.prototype.pos = function (x, y, w, h)
{
	this._x = x;
	this._y = y;
	this._width = w;
	this._height = h;
};

// Out main function
function setStage ()
{
	var sw:Number = Stage.width;
	var sh:Number = Stage.height;
	// Calculate percentages based on Stage dimensions
	var Lw:Number = sw / 100 * leftColWidth;
	var Mw:Number = sw / 100 * mainColWidth;
	var Rw:Number = sw / 100 * rightColWidth;
	var Hp:Number = sh - (headHeight + footHeight);
	// Position / resize our MovieClips
	header.pos (0,0,sw,headHeight);
	leftCol.pos (0,headHeight,Lw,Hp);
	mainCol.pos (Lw,headHeight,Mw,Hp);
	rightCol.pos (Lw + Mw,headHeight,Rw,Hp);
	footer.pos (0,sh - footHeight,sw,footHeight);
}

// Add a listener
var stageListener:Object = new Object ();
Stage.addListener (stageListener);

// When the Stage dimensions change...
stageListener.onResize = setStage;

// Call our function to set it all up right!
setStage ();
Posted on 08 Mar 2007
Posted in
Tagged
57 Comments
0 Trackbacks

Meta

Dynamic Positioning Continued was posted on March 8th 2007 in the category Code / Code, Flash and tagged; , , .

You can Leave a comment.

Twitter <follow>

February 7th 2012 - 5:37pm

RT @_leonardsouza_: 37signals to stop developing for IE8 » http://t.co/wL6dtZSg

Discussion

57 Responses to Dynamic Positioning Continued

1 2 3 ... 5

Leave a Reply

  1. John 4 years ago

    How do I embed this to an html file? I tried simply using “embed” tags, but it doesn’t get resized. And oh yeah, thanks for this great resource. :)

    Reply to this comment

  2. kristian 4 years ago

    interesting, I used your prototype to resize an arbitrary box in the center of the stage, and when i test it, the resize listener wont kick in till after I resize the window. so basically when I test the movie, the box is huge, and then i drag, and everything goes into place.

    i used to do fullscreen flash the same way as this, except i had an if then statement: if 800 then do this. and in a way that was the workaround to the listener needing an input, how is that problem solved here?

    Reply to this comment

  3. kristian 4 years ago

    ps, some of my if then code example was omitted. dont worry about it — that random 800 was not important.

    Reply to this comment

  4. Goose 4 years ago

    nice example

    Reply to this comment

  5. Soulwire 4 years ago

    Hi Kristian,

    To solve your first problem (elements not positioning until stage is resized) simply put all of your calculations for position/size into a function (i.e. ‘setStage()‘ as above). You can then call this function when the stage is resized, but also from the frame where your elements are added to the stage. This will position them according to your rules without the listener being triggered.

    To vary the results for different resolutions, as you say you can set up a conditional if/else statement. You can detect the users resolution with:

    System.capabilities.screenResolutionX;
    // And of course
    System.capabilities.screenResolutionY;
    

    However, I recommend going by the stage properties, as they will represent the actual size of the broswer window.

    Better still, use percentages and relative positioning, so that your stage elements will be positioned proportionally no matter what resolution the user has.

    Hope this helps.

    Reply to this comment

  6. Soulwire 4 years ago

    John,

    for embedding flash you can use SWF object http://blog.deconcept.com/swfobject/

    It is nice and clean and your pages will validate because it avoids nasty embed code.

    Also, you can use automatic update for newer versions of flash player! cool :)

    Reply to this comment

  7. Luis 4 years ago

    This has saved my butt! thanks! :)

    Reply to this comment

  8. Patrick 4 years ago

    This question may have been answered above, but Ill ask it anyway.

    I can get the resize to work, and positioning going just fine.

    What I can’t do is get a movie clip, positioned at the top left of the screen, to do a resize to scale, to cover the entire movie clip. I want to use a background image, that resizes to cover the entire browser, and when the user resizes the browser, that image doesnt get warped.

    Can anybody help?

    Reply to this comment

  9. vika_1987@aol.com 4 years ago

    Hello, very nice, good Luck!

    Reply to this comment

  10. Wonkyrat 4 years ago

    I love this function but I am unable to put it into practice on a website layout.

    I’m trying to have a menu system in the header that opens new mc’s in the content.

    But this is proving impossible to me… :(

    I put my menu sytem in the header mc and it just stretches it, how can I fix it??

    I’ve looked everywhere for help but just cant find it, please help??

    Thanks

    Reply to this comment

  11. Eugene 4 years ago

    It was verrry helpful!

    Thanks!

    Reply to this comment

  12. ruggero 4 years ago

    Hi Just…

    Nice example – and some kind of math-test you put me on: I didn’t get the point and failed your exam.;-)

    Anyway, I’ll try recall what I wrote on my way-gone message…

    Two big challenges:

    • how do I create a liquid textfield that keeps fontsize while resizing the window?
    • any solution on how to accomplish this without using scrollbars, why the textfield boundaries will cut the textfield content (very-long-text) creating some kind of ‘linked pages array’ for the remainding sliced-off content?

    All my best….keep me posted

    Reply to this comment

1 2 3 ... 5