AS3 FullScreen Stage Resizer and Align Class
This is a simple utility class that resizes a DisplayObject to fill the stage area and align based on predefined positions such as top, top-left, top-right, bottom, bottom-left, bottom-right, center, center-left, center-right. There’s a lot of debate wheter or not to use private static on as3, i figured that if the class compiles with no errors, we are good to go.
package com.guinetik.kinetic.utils
{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.display.Stage;
/**
* ...
* @author Gui
*/
public class DisplayObjectUtilsResizer
{
public static function resizeProp(align, obj:DisplayObject):void
{
if (obj.stage != null)
{
var sH:Number = obj.stage.stageHeight;
var sW:Number = obj.stage.stageWidth;
switch(align)
{
case "T": resizeTop(obj, sW, sH); break;
case "TL": resizeTopLeft(obj, sW, sH); break;
case "TR": resizeTopRight(obj, sW, sH); break;
case "C": resizeCenter(obj, sW, sH); break;
case "CL": resizeCenterLeft(obj, sW, sH); break;
case "CR": resizeCenterRight(obj, sW, sH); break;
case "B": resizeBottom(obj, sW, sH); break;
case "BL": resizeBottomLeft(obj, sW, sH); break;
case "BR": resizeBottomRight(obj, sW, sH); break;
}
} else throw new Error("the object is not on the stage");
}
static private function resizeBottomRight(obj:DisplayObject, sW:Number, sH:Number):void
{
obj.width = sW;
obj.scaleY = obj.scaleX;
if (obj.height < sH)
{
obj.height = sH;
obj.scaleX = obj.scaleY;
}
obj.x = int((obj.stage.stageWidth - obj.width));
obj.y = int((obj.stage.stageHeight - obj.height));
}
static private function resizeBottom(obj:DisplayObject, sW:Number, sH:Number):void
{
obj.width = sW;
obj.scaleY = obj.scaleX;
if (obj.height < sH)
{
obj.height = sH;
obj.scaleX = obj.scaleY;
}
obj.x = int((obj.stage.stageWidth - obj.width)/2);
obj.y = int((obj.stage.stageHeight - obj.height));
}
static private function resizeBottomLeft(obj:DisplayObject, sW:Number, sH:Number):void
{
obj.width = sW;
obj.scaleY = obj.scaleX;
if (obj.height < sH)
{
obj.height = sH;
obj.scaleX = obj.scaleY;
}
obj.x = 0;
obj.y = int((obj.stage.stageHeight - obj.height));
}
static private function resizeCenterRight(obj:DisplayObject, sW:Number, sH:Number):void
{
obj.width = sW;
obj.scaleY = obj.scaleX;
if (obj.height < sH)
{
obj.height = sH;
obj.scaleX = obj.scaleY;
}
obj.x = int((obj.stage.stageWidth - obj.width));
obj.y = int((obj.stage.stageHeight - obj.height) / 2);
}
static private function resizeCenter(obj:DisplayObject, sW:Number, sH:Number):void
{
obj.width = sW;
obj.scaleY = obj.scaleX;
if (obj.height < sH)
{
obj.height = sH;
obj.scaleX = obj.scaleY;
}
obj.x = int((obj.stage.stageWidth - obj.width) / 2);
obj.y = int((obj.stage.stageHeight - obj.height) / 2);
}
static private function resizeCenterLeft(obj:DisplayObject, sW:Number, sH:Number):void
{
obj.width = sW;
obj.scaleY = obj.scaleX;
if (obj.height < sH)
{
obj.height = sH;
obj.scaleX = obj.scaleY;
}
obj.x = 0;
obj.y = int((obj.stage.stageHeight - obj.height) / 2);
}
static private function resizeTopRight(obj:DisplayObject, sW:Number, sH:Number):void
{
obj.width = sW;
obj.scaleY = obj.scaleX;
if (obj.height < sH)
{
obj.height = sH;
obj.scaleX = obj.scaleY;
}
obj.x = int((obj.stage.stageWidth - obj.width));
obj.y = 0;
}
static private function resizeTop(obj:DisplayObject, sW:Number, sH:Number):void
{
obj.width = sW;
obj.scaleY = obj.scaleX;
if (obj.height < sH)
{
obj.height = sH;
obj.scaleX = obj.scaleY;
}
obj.x = int((obj.stage.stageWidth - obj.width) / 2);
obj.y = 0;
}
static private function resizeTopLeft(obj:DisplayObject, sW:Number, sH:Number):void
{
obj.width = sW;
obj.scaleY = obj.scaleX;
if (obj.height < sH)
{
obj.height = sH;
obj.scaleX = obj.scaleY;
}
}
}
}
You can apply to your code using a simple stage resize listener like this:
import com.guinetik.kinetic.utils.DisplayObjectUtilsResizer;
import flash.events.Event;
stage.align = "TL";
stage.scaleMode = "noScale";
stage.addEventListener(Event.RESIZE, onStageResize);
function onStageResize(e:Event):void
{
DisplayObjectUtilsResizer.resizeProp("TL", myImage);
}