Skip to content

Instantly share code, notes, and snippets.

@safareli
Created November 19, 2013 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save safareli/7547868 to your computer and use it in GitHub Desktop.
Save safareli/7547868 to your computer and use it in GitHub Desktop.
package {
public class main extends ResponsiveScene {
public function shadowExam(){
m_scene = scene;
}
override public function onReady():void{
trace("onReady");
trace(m_scene.name);
}
}
}
package {
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
// extend ResponsiveScene from main class and
// set m_scene to MovieClip object with sould be scaled
public class ResponsiveScene extends MovieClip {
public var m_scene:MovieClip;
//override public function onReady():void{
public function onReady():void{
trace("onReady should be overridden");
}
public function ResponsiveScene() {
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
private function addedToStage(event:Event):void{
if(!m_scene) return trace("ResponsiveScene.m_scene must be set");
removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE, resizeHandler);
onReady();
}
function resizeHandler(e:Event):void{
resizeMe(m_scene,stage.stageWidth,stage.stageHeight);
}
//The resizing function
// parameters
// required: mc = the movieClip to resize
// required: maxW = either the size of the box to resize to, or just the maximum desired width
// optional: maxH = if desired resize area is not a square, the maximum desired height. default is to match to maxW (so if you want to resize to 200x200, just send 200 once)
// optional: constrainProportions = boolean to determine if you want to constrain proportions or skew image. default true.
function resizeMe(mc:MovieClip, maxW:Number, maxH:Number=0, constrainProportions:Boolean=true):void{
maxH = maxH == 0 ? maxW : maxH;
mc.width = maxW;
mc.height = maxH;
if (constrainProportions) {
mc.scaleX < mc.scaleY ? mc.scaleY = mc.scaleX : mc.scaleX = mc.scaleY;
}
//center the image in the area
mc.x = maxW/2 - mc.width/2;
mc.y = maxH/2 - mc.height/2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment