How to adjust your flash to the display size ?

You can define how your swf is resized in your container (html, ..) by using :

stage.scaleMode = StageScaleMode.NO_SCALE;

stage.scaleMode can be :

  • StageScaleMode.EXACT_FIT—The entire application is visible in the specified area without trying to preserve the original aspect ratio. Distortion can occur, and the application may appear stretched or compressed.
  • StageScaleMode.SHOW_ALL—The entire application is visible in the specified area without distortion while maintaining the original aspect ratio of the application. Borders can appear on two sides of the application.
  • StageScaleMode.NO_BORDER—The entire application fills the specified area, without distortion but possibly with some cropping, while maintaining the original aspect ratio of the application.
  • StageScaleMode.NO_SCALE—The entire application is fixed, so that it remains unchanged even as the size of the player window changes. Cropping might occur if the player window is smaller than the content.

If you want to control resize of each clip you have into your flash, use StageScaleMode.NO_SCALE. Be careful to ajust all clip in your flash ! “Cropping might occur if the player window is smaller than the content.”

You may define the align of the stage :

stage.align = StageAlign.TOP_LEFT;

More informations on align stage at http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#align

Then add a listener for resize event :

// import flash.events.Event;
// import flash.display.StageScaleMode;
// import flash.display.StageAlign;

// Dispatched when the scaleMode property of the Stage object is set
// to StageScaleMode.NO_SCALE and the SWF file is resized.
stage.addEventListener(Event.RESIZE, stageHasBeenResized);

And here’s how to resize your clips depending on the size of the stage :

function stageHasBeenResized(e:Event):void {
	trace("stageWidth: " + stage.stageWidth + " stageHeight: " + stage.stageHeight);

	// clip always repositionned to the top left of the stage
	clipAlways_TOP_LEFT.x = 0;
	clipAlways_TOP_LEFT.y = 0;

	// clip always repositionned to the bottom right of the stage
	clipAlways_BOTTOM_RIGHT.x = stage.stageWidth - clipAlways_BOTTOM_RIGHT.width;
	clipAlways_BOTTOM_RIGHT.y = stage.stageHeight - clipAlways_BOTTOM_RIGHT.height;

	// clip always center (horizontal & vertical) of the stage
	clipAlways_CENTER.x = (stage.stageWidth - clipAlways_CENTER.width)*0.5;
	clipAlways_CENTER.y = (stage.stageHeight - clipAlways_CENTER.height)*0.5;

	// clip with 80% width of the stage width
	clipAlways_80p_WIDTH.width = stage.stageWidth*0.8;

	// clip with a fixed large, may be cropped
	clip_FIXED_WIDTH.with = 1000;
}

If you are publishing for mobile device, you may be interested by the screenDPI (flash.system.Capabilities.screenDPI).
See this post : How to resize clip in millimeters or inches ?

How to resize clip in millimeters or inches ?

Every device don’t have the same DPI. Also if you want a clip with 2 inches wide on every device screen you have to convert inches/millimeters to pixel :

// import flash.system.Capabilities;

// clip 2" wide
clip_2_w.width = inchesToPixels(2);

// clip 20mm wide
clip_20_w.width = mmToPixels(20);

function inchesToPixels(inches:Number):int {
   return Math.round(Capabilities.screenDPI * inches);
}

function mmToPixels(mm:Number):int{
   return Math.round(Capabilities.screenDPI * (mm / 25.4));
}

But… if I measure with my ruler, the dimensions are not good! :( Yes that true, but this trick will save you when you get a tiny result on iPhone or Android.

How to optimize a for loop

Worst for loop :

for(var i=0; i<myTab.length; i++){
	//action here
	var lolVar = i;
}

Best for loop :

var i:int;
var tabL:int = myTab.length;
var lolVar:int;
for(i=0; i<tabL; i++){
	lolVar = i;
}

Why is it better ?
Declaring variable is long also put them outside the loop !
Getting the length of an array is quite long too. Also if the loop doesn’t change the array, put it outside the loop :)

You may declare var in one line :

var i:int, tabL:int = myTab.length, lolVar:int;
for(i=0; i<tabL; i++){
	lolVar = i;
}

Declaring variable in one line is faster, but harder to read and debug ;)

Calling clip with an int variable

Imagine you have many clip named :

clip_1
clip_2
..
clip_10

And you want to do some actions to them. Too long and boring to add manualy the action. The tips is to use a for loop :

var i:int=0;
for(i=0; i<11; i++){
	// you have to put a reference to the parent : this. if there is no parent
	this["clip_"+i].alpha = 0.5;
}

If clips are in mcParent :

var i:int=0;
for(i=0; i<11; i++){
	// this. is optionnal
	this.mcParent["clip_"+i].alpha = 0.5;
}

Hope next time you will name your clip with number for fast call ;)

How to move a clip into an other clip ?

Exemple : you have container_1, my_mc and container_2. my_mc is a child of container_1. I want to move my_mc into container_2. What to do ? Copy this clip and delete the old one ? No !
Simply do this :

container_2.addChild(my_mc);

It will automatically detele my_mc in container_1 and add my_mc in container_2. That’s all :)

How to remove all child in a clip ?

Quite simple to remove all child in a display container :

// mc can be a MovieClip, Sprite, Loader, ..
removeAllChild(mc);

function removeAllChild(mc:DisplayObjectContainer):void{
	while(mc.numChildren>0){
		mc.removeChildAt(0);
	}
}

Hello world

Hey ! Opening this blog.. Hmmm. First I want to say why I open this blog :
- To share my experience as as3 developper (and get yours ?)
- Work my english (oh yeah I’m french)
I will write some articles about cool snippet code in actionscript 3 but not only. May be some times about geeky things, tools for web…
So if you have some comment, correct my english, give optimisation of the code, etc.. come on post it !
Hope this blog will have some activity (depend on me ^^) !
See you soon :)