
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.
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 ();
34 Responses to Dynamic positioning continued
John
May 24th 2007
6:19 pm
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. :)
kristian
Jun 11th 2007
10:01 am
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?
kristian
Jun 11th 2007
10:04 am
ps, some of my if then code example was omitted. dont worry about it — that random 800 was not important.
Goose
Jun 11th 2007
10:18 am
nice example
Soulwire
Jun 11th 2007
10:18 am
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:
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.
Soulwire
Jun 11th 2007
6:24 pm
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 :)
Luis
Jun 29th 2007
11:16 am
This has saved my butt! thanks! :)
Patrick
Jul 11th 2007
7:35 am
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?
vika_1987@aol.com
Aug 18th 2007
11:34 am
Hello, very nice, good Luck!
Wonkyrat
Aug 22nd 2007
6:07 pm
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
Eugene
Sep 24th 2007
4:41 pm
It was verrry helpful!
Thanks!
ruggero
Nov 18th 2007
6:29 pm
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:
All my best….keep me posted
Dave
Nov 20th 2007
5:52 pm
Sweet! Just what I was looking for - some nice actionscript for me to sink my teeth into :D
Chris
Nov 26th 2007
11:22 am
hmm yes
is there anyway to set the center box a static width say 500, and have the sides just fill out to the ends of the browser? this way your main content only grows hight wise not width butthe screen always looks full because the two outside cols will adapt..
Mike
Nov 26th 2007
7:18 pm
Hey,
Awesome script! Just was I’ve been looking for, I was doing it a much longer harder way so this is saving me lots of time and well frankly it’s much more efficent then how I was doing it.
I do have a question or two though, I’m going to give you a website which I believe is using this or something very similar (beautifully done website too)
http://www.stylusmodels.com/
My first question is how to-do a background that dosn’t scale, but repeats the way this site is done. You can resize the window but the background always looks beautiful.
The second is the menu at the top. It dosn’t resize scale (the middle of it) it stays the same, the only part that resizes or scales are the far outsides to give it the faded off effect…
and thrid, as someone else above asked, how do I create a liquid textfield that keeps fontsize while resizing the window?
If you (or anyone else) is able to provide some type of quick answer that would be amazing. Hope to hear from someone soon. Thanks.
-Mike
Mike
Nov 26th 2007
7:50 pm
sorry one more,
is there also any way to have the center box a fixed width and the outside fluid?
Soulwire
Nov 27th 2007
6:40 pm
Hi Mike, Chris and Ruggero,
You can download another example here which hopefully addresses your questions.
Stage listeners update
In the source is an example of a texfield which resizes its height to the stage. If the stage height is not big enough to display all the text, it will scroll into another column. I have also put a switch in there which will let you add a ‘more’ button if you preffer that method.
The FLA also includes and example of how to achieve the tiling background effect. It uses a bitmap from the library and creates a seamless tile to the exact size of the stage (without scaling). It looks smart, you’ll like it!
Also in the new example is a centred, fixed width box which will adjust it’s height to the stage, plus another fixed width box which has fading boxes attached to either side of it which resize, leaving the main box fixed.
Chris, when you say “…have the sides just fill out to the ends of the browser” - I hope I have covered this for you and Mike in the new FLA with the centred box that has scrolling outside tabs, but let me know if I’ve misunderstood.
Hope this helps guys.
Tim
Dec 2nd 2007
10:03 am
Yo, sup, props for the wicked example m8, i wuz cheken out an above link to some model website good shit, how did you do the fade/lighting effect? around the edges of the background, it looks like faded out & its bright in th emiddel ………. can u throw up an example of that?
Soulwire
Dec 7th 2007
6:42 pm
Hey Tim.
The fading is simply done with a transparent to opaque ( or translucent) circular gradient. Or possibly the reverse - i.e. a light coloured gradient which fades out towards the edges. Either way, if you layer this over your background with a low alpha or even a blendmode applied you can create an effect similar to the lighting in After Effects. Just stretch this movieClip to the stage width and height when your stage resizes and you’re done ;)
Appi
Dec 17th 2007
6:43 pm
Hi Justin,
I am new with as3 and I am trying to build my portfolio. I a couple of questions that you might be able to answer.
First, for this fullbrowser, which stage dimenstion in the flash movie I should use …. let’s say 800×600.
The second, what about if you do not want the elements to be centered. What about if you want them in one side of the browser and change position according with browser dimension.
I hope you could give me some advice on this questions and thanks so much for sharing this tutorials.
Soulwire
Dec 17th 2007
8:45 pm
Hi Appi,
To answer your first question; it really doesn’t matter what original resoluton you use. If you are setting your stage mode to ‘noScale’, then you could have a 10 x 10px movie and would still get the same effect as 1280 x 1024. The important thing is the size of the user’s browser window.
If you want elements to not be centered, set the stage align to ‘TL’ (top left) as I have done in the example above. Now, you can set an absolute or relative position, say, from the left. For example, if you want a MovieClip called logoMc to always sit a quarter of the stage width from the left, set it to ‘logoMc._x = stage.width * .25;‘. So work in percentages and you will be ok ;)
Hope that clears things up.
Appi
Dec 18th 2007
6:47 pm
Hi Justin,
Thanks for your comments. It seems clear to me but the code did not respond as I would. Could you please have a look at it.
Thanks!
Mike
Jan 13th 2008
11:11 am
Hey,
How would you use a preloader, that’s centered, then it loads an external swf which also follows all the same rules? centers width & height, etc etc..
Arthur
Feb 8th 2008
5:21 pm
Hey. Nice site man! I’m trying to rescale an image according to stage dimensions. So when the browser is resized, the image would scale proportionally. Is there a way to accomplish this?
Soulwire
Feb 12th 2008
6:52 pm
Hi Arthur,
To resize an image and retain the correct ratio, you can use this function. The image will be cropped if the proportions of the browser are extreme, but the image will not distort.
Hope this helps.
Wahiche
May 7th 2008
9:24 pm
I have to say thank you for this script is is fantastic. I have been looking for something like this for a long time. I am not a very talented actionscripter as my skills lie mainly in the design side, and I am not too great at that either…lol. Just being hard on myself.
But I do have a question regarding the code here. I have added more code to it to change the layout and to add an image in one of the containers. All my code works fine as it seems but I find that when I compile it that the image does not scale properly until I resize the stage. Can you help please?
I have attached my version of your code so you can see what I have done.
Thanks again for being a genius.
Soulwire
May 8th 2008
9:38 am
Hi Wahiche.
Thanks for your comment.
Have you tried calling the setStage method from inside the onLoadInit function? Currently in your code there is a call to setStage before the pic is loaded, so this function won’t fire again until the stage is resized, as you’ve been observing.
Also, inside onLoadInit you set the width and height of the movieClip containing the image, but if you want this to resize with the stage you should move this to the setStage function and then call from , if that makes sense?
Like so:
Hope that helps :)
Wahiche
May 8th 2008
7:27 pm
Thank you for your prompt response but I think what i am trying to do is a little more complicated and if I explained it you may have another suggestion.
I am trying to make it so that the image gets resized dependant on the width constraint of the container it is in (leftCol). There is a max of 50% of the stage for the width, now I want it to resize the image with the proper aspect ratio.
this is my new code after a lot of reworking, so I hope that it is understandable. I have made some of the Variables global as I needed to be able to call them in and out of Functions. I also have it set now to resize but it just resizes at some points at the right aspect ratio but does not work properly. I also have a question about the mc that is being created to hold the image. If you do a trace on the mc._width and mc._height you will see that any image in there is always said to have the dimensions multiplied 4:3, why?
Thank you for all your help and not sure if you ever go on MSN to talk to people about this but if you do i would be glad to have a conversation with you about it. I think you are a genius and would just really like to pick your brain.
Wahiche
Rob
Jun 1st 2008
5:05 pm
First of all, love the script; had it working great in AS2 with no problems, but now I’m working in AS3 and haven’t got it to work, don’t suppose you have an AS3 version?
Thanks.
Soulwire
Jun 1st 2008
5:59 pm
Hi Rob,
No worries! I’ve ported the download above to AS3 for you, enjoy…
http://blog.soulwire.co.uk/uploads/2007/03/stage-listeners-as3.zip
Rob
Jun 2nd 2008
7:04 pm
Thanks, that’s awsome!
Ben
Jul 8th 2008
3:43 pm
Hey,
Thanks for a great tutorial. I’ve been playing about with liquid layouts (still using AS2), and I’ve got a quick question. I’ve got a movie clip that floats to the right of the browser window, so as the browser is made smaller the movie clip moves in with it. This is fine, but what I want to achieve is when the movie clip reaches a certain position (the browser window gets too small), it stops floating and becomes fixed. I’ve used the following code:
While this kind of works, once the browser becomes too small the movie clip constantly jumps from floating to the position I want it. Any ideas? I’m fairly new to AS so any help is greatly appreciated.
Cheers.
Soulwire
Jul 13th 2008
6:19 pm
Hi Ben,
Have you tried something like:
Ben
Jul 17th 2008
9:53 am
Hi Soulwire,
A massive thank you for that, worked a treat, and I learnt something as well : )
Cheers, Ben
Leave a Reply