
Since fullscreen flash websites have become so popular; this little trick (a necessity really) has been used to make sure that no matter what screen resolution a user has, they can experience the site in the way in which the designer intended.
I realise that listeners are common knowledge, but I have had a lot of emails asking about dynamic positioning so I have written this brief tutorial on how to get all the elements in your Flash movie to position themselves automatically when the user’s browser window resizes, or if they are using a different screen resolution to you and you hate the idea of letting the movie scale!
The principal is simple, like in CSS with fluid layouts, you effectivelly position everything relitively, and write some simple formulas for maintaining their relitivity no matter what size stage they are on.
You can also apply this to the height and width of, for example, the background of a banner, so that your pixel font still looks crisp and nicely aligned as the bar behind is stretching to fit the window. The permutations are endless, and evidently, this is a really useful and basic tool to have when putting together a Flash site. I have even used it for CD Business Card presentations, to make sure text areas and graphics sit nicely in harmony with each other, even if it found its way into the grubbly hands of an 800 x 600 user.
Anyway, the code - Just paste this into the Actionscript panel, render and enjoy. Drag the movie’s resize handles around for some orgasmic repositioning pleasure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | // Setup the Stage properties Stage.scaleMode = "noScale"; Stage.align = "TL"; // Create some dummy boxes // This is only for the demonstration var margin:Number = 10; var boxSize:Number = 100; var boxes = [ ["top", "0x9933cc"], ["right", "0x00ff00"], ["bottom", "0xff0000"], ["left", "0x0099ff"], ["center", "0xff3399"]]; for (var i = 0; i < boxes.length; i++) { var newBox:MovieClip = createEmptyMovieClip (boxes[i][0], getNextHighestDepth ()); var nameBadge = newBox.createTextField ("test_txt", 1, 0, (boxSize / 2) - 10, boxSize, 10); nameBadge.autoSize = "center"; nameBadge.text = boxes[i][0]; nameBadge.textColor = 0xffffff; with (newBox) { beginFill (boxes[i][1],100); moveTo (0,0); lineTo (boxSize,0); lineTo (boxSize,boxSize); lineTo (0,boxSize); lineTo (0,0); endFill (); } } /* The core function... I keep this separate from the 'onResize' handler so that I can call it at the start to position all of my elements if the screen size has remained the same */ function setStage () { var sw:Number = Stage.width; var sh:Number = Stage.height; /* Rule for Top / Center alignment */ top._x = sw / 2 - top._width / 2; top._y = margin; /* Rule for Right / Middle alignment */ right._x = sw - (right._width + margin); right._y = sh / 2 - right._height / 2; /* Rule for Bottom / Center alignment */ bottom._x = sw / 2 - bottom._width / 2; bottom._y = sh - (bottom._height + margin); /* Rule for Left / Middle alignment */ left._x = margin; left._y = sh / 2 - left._height / 2; /* Rule for Center / Middle alignment */ center._x = sw / 2 - center._width / 2; center._y = sh / 2 - center._height / 2; } // 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 (); |
Have fun!
13 Responses to Dynamic Positioning
sergio
Mar 7th 2007
5:47 pm
hi, i been looking for this solution…. but, the thing is im not really an expert on actionscript, would there be any posibility of getting an example with movie clips? maybe you can send one to my e-mail???
thanks,
sergio.
estela
Jun 20th 2007
5:50 pm
hello soulwire
the example you gave is a great one, thank you for that!
but… like sergio (above) i am not an expert on actionscript either and it would help us a lot if you used an movie clip in your file as an example. not that your code needs a movie clip to run because it work just fine, but i wouldn’t know how to attach a movie clip to it. (let’s say instead of the purple box i wanted it to be a logo that was always on the center top).
i would appreciate if you could do that.
thank you very much.
:)
estela
Soulwire
Jun 20th 2007
5:54 pm
Hi Estela
I’ve posted an update with source code, hope this helps.
http://blog.soulwire.co.uk/flash/actionscript-2/dynamic-positioning-part-two/
estela
Jun 20th 2007
8:22 pm
thank you!!
you rock
:)
estela
Jul 25th 2007
5:56 pm
JUSTIIIIIIIIIIN!
hey it’s estela again.
thank you so much for your help with this tutorial, you helped a lot!
what brings me back is: i got very ambitious with the idea of working with fluid layouts i wanted to use in my next project, but i am having trouble with layers now.
basically i have a project that have 3 flash layers. all of them are kind of centered… when i call a new file to be loaded on the third layer it loads on the top left and then moves to the center. i am using:
any idea how to solve this?
if you have the time… please take a look, i created a link for you to download the basic files:
http://www.estelavasco.com/justinrocks.zip
any advice will appreciated.
thanks again, you rock.
estela.
Jerry
Feb 18th 2008
12:57 pm
hi. just wanna ask where do you actually paste the code in Flash.. thanks.
Alexandra
Apr 25th 2008
5:58 pm
Can we have the same .fla but that loads an external .swf and aligns it to the right all the time on resize also?
I have been looking all over the Web to get the code. Must be simple but I a designer not a programmer.
Any help would really be appreciated.
Alexandra
Apr 25th 2008
6:00 pm
The problem is that it is a horizontal animation from right to left that starts outside the stage. So onResize using the width of the satge minus the movie clip width which is large simply can’t work as the x and Y coordinates fall way outside the stage.
Soulwire
Apr 29th 2008
6:22 pm
Hi Alexandra,
To align a loaded swf, just load it into a MovieClip container using loadMovie and then apply any transformations to that movieclip.
In terms of content outside of the clips boundary messing up your resize logic; one simple solution would be to add a static movieclip with an instance name as a child of the movieclip you are resizing, then refference that to get the dimensions. another alternative would be to store the original dimensions in a variable before you start adding children. Finally, the simplest option if its possible would be to specify a constant for the dimensions, if you don’t need to determine this at runtime.
ixiel
Jul 9th 2008
6:40 pm
hi soulwire,
great script! erm.. wanna ask what if we set the stage.align to “T”.. hows the syntax gonna looks like?
the problem with my flash is that there are so many mcs to be repositioned/actionscripted manually, is there a way to automatically centred mcs without giving names to all the mcs.. like stage.align=”T” ?
thx..
Soulwire
Jul 13th 2008
5:43 pm
Hi Ixiel.
Setting the stage align to ‘T’ would align the swf top center, but leaving it unset would mean that your content is centered (as in any available extra space on resize forms padding around it).
If you want to actually move, say, all MovieClips on the stage (or within another MovieClip) to the same place, to save putting an instance name on them all and then having to target them specifically, you could use a function like this as a shortcut:
This looks for ALL the MovieClips in a container element and then applies the same reposition logic to all of them (AS2).
ixiel
Jul 15th 2008
7:01 pm
Hi Soulwire,
Thanx for the script. Unfortunately the script doesn’t work correctly for my flash.
But if we use your tutorial script as example, if i change the Stage.align = “TL” to “T”, the 5 boxes no longer positioned correctly. How do you fix it?
Thx.
Soulwire
Jul 15th 2008
10:40 pm
Hi Ixiel,
This is because when the stage is aligned top left (’TL’) the upper left corner is point 0,0 in the coordinate space, therefore any calculations we do use this as the reference point. So 50% of the stage width is 50% of the way from the top left corner to the far right. However, setting it to ‘T’ means that the reference point is always half of the added space from the left. Example; if the movie width was 500px originally and you resize the window to 1000px width, then the reference point considered 0,0 would actually be at x:250px (half of the difference). The problem this causes for the script is that Stage.width would still return 1000px, so a movieclip at x:75% (for example) would be 75% of 1000 (750) pixels from x:250px = 1000px!! So you see, this aint good.
To keep things simple, I would recommend using ‘TL’, this way you can always know where your reference point is. Regarding the script I sent you; it is tried and tested and worked ok for me. Perhaps you want to send me an fla if you’re really stuck; I’ll have a look if I’ve got the time => justin |[at]| soulwire dot co dot uk
Leave a Reply