AS3: Dynamic Positioning Class
October 22nd, 2010Dont have time reading all this? Download source here
For your fullscreen flash website to look as you want in every resolution you need to use dynamic positioning of your movieclips. I and Henrik Porseland put together this yesterday, most for our own sake but also to share with others. It’s a simple class where you can place object just by typing “left” and “top” och “middle” and “bottom”. Let me show you some examples of what you can do!
First of all you need to import the class into your project and set some stage properties. Do that by this code:
import com.blacksmiths.DynamicPositioning; var position:DynamicPositioning = new DynamicPositioning(); var setPosition = position.setPos; stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE;
Now you need a resize handler for your object to change position when window is resized. Add this to the bottom your code:
function resizeHandler(e:Event){
// Nothing yet
}
stage.addEventListener(Event.RESIZE, resizeHandler);
stage.dispatchEvent(new Event(Event.RESIZE));
To position your objects just add this in your resizeHandler function:
position.setStageSize(stage.stageWidth,stage.stageHeight); setPosition(box1, "left", "top"); setPosition(box2, "right", "top"); setPosition(box3, "left", "bottom"); setPosition(box4, "right", "bottom"); setPosition(box5, "middle", "middle");
This code will place the objects exactly like the image on top of this post. One in every corner and one i the middle. Just like the code saids!
You can tweak it how you want it. Place by number or word or both. Here is some examples how you can use the Class:
setPosition(box, "middle", "bottom-20"); // center and 20px from bottom setPosition(box, "right-100", 20); // 100px from right and 20px down setPosition(box, "right", "top"); // in the top right corner
For this to work you need to download the class, for an example just download the source.
It’s very easy and quick to use. Please try it out and we’ll be happy for some feedback.
Download source for DynamicPositioning
Thank you!


January 26, 2011
URL
This class is awesome! I will def. use it in my projects later on! :)