3. The first part of code that we will tell the flash player to keep the size of all the elements the same regardless of the screen size. By default the stage will scale up to fill the screen and we don't want that to happend.
var swfStage:Stage = this.stage; swfStage.scaleMode = StageScaleMode.NO_SCALE; swfStage.align = StageAlign.TOP;
4. Next we will import some flash libraries required for the cool elastic movement effect when repositioning the stage elements.
import flash.events.*; import fl.transitions.Tween; import fl.transitions.TweenEvent; import fl.transitions.easing.*;
5. Next we will create a variable that will tract the state of the stage (fullscreen of normal)
var screenCheck:Boolean = false;
6. We will add a listener for a click event to the center movie clip that will switch the stage to full screen.
center.addEventListener(MouseEvent.CLICK, fullScreenUP);
7. Now we will create a tween function to align the stage elements. This function will move the target movie clip (TweenMC) to the TargetX and TargetY coordonates using an Elastic.easeInOut movement.
function positionMc(TweenMC:MovieClip,TargetX:Number,TargetY:Number):void { var tween_handler_1 = new Tween(TweenMC,"x",Elastic.easeInOut,TweenMC.x,TargetX,1,true); var tween_handler_2 = new Tween(TweenMC,"y",Elastic.easeInOut,TweenMC.y,TargetY,1,true); }
8. We will run this fuction on all the satge elements for the initial positionig on the stage. I have arranged the elements so they leave a 20 px border all arround. You can changed this to your needs. Also 550 is the stage width I used. You will have to change this as well if it is different.
positionMc(top_left,((stage.stageWidth-550)/2*-1)+20,20); positionMc(middle_left,((stage.stageWidth-550)/2*-1)+20,stage.stageHeight/2-middle_left.height/2); positionMc(bottom_left,((stage.stageWidth-550)/2*-1)+20,stage.stageHeight-bottom_left.height-20); positionMc(middle_top,stage.stageWidth/2-middle_top.width/2-(stage.stageWidth-550)/2,20); positionMc(center,stage.stageWidth/2-center.width/2-(stage.stageWidth-550)/2,stage.stageHeight/2-center.height/2); positionMc(middle_bottom,stage.stageWidth/2-center.width/2-(stage.stageWidth-550)/2,stage.stageHeight-bottom_left.height-20); positionMc(top_right,stage.stageWidth-top_right.width-20-(stage.stageWidth-550)/2,20); positionMc(middle_right,stage.stageWidth-middle_right.width-20-(stage.stageWidth-550)/2,stage.stageHeight/2-middle_right.height/2); positionMc(bottom_right,stage.stageWidth-bottom_right.width-20-(stage.stageWidth-550)/2,stage.stageHeight-bottom_right.height-20);
9. Now we will add an event listener for when the stage is resized and the function for it that has the same code as just above to position all the elements.
stage.addEventListener(Event.RESIZE,fullScreenCenter); function fullScreenCenter(e:Event):void { positionMc(top_left,((stage.stageWidth-550)/2*-1)+20,20); positionMc(middle_left,((stage.stageWidth-550)/2*-1)+20,stage.stageHeight/2-middle_left.height/2); positionMc(bottom_left,((stage.stageWidth-550)/2*-1)+20,stage.stageHeight-bottom_left.height-20); positionMc(middle_top,stage.stageWidth/2-middle_top.width/2-(stage.stageWidth-550)/2,20); positionMc(center,stage.stageWidth/2-center.width/2-(stage.stageWidth-550)/2,stage.stageHeight/2-center.height/2); positionMc(middle_bottom,stage.stageWidth/2-center.width/2-(stage.stageWidth-550)/2,stage.stageHeight-bottom_left.height-20); positionMc(top_right,stage.stageWidth-top_right.width-20-(stage.stageWidth-550)/2,20); positionMc(middle_right,stage.stageWidth-middle_right.width-20-(stage.stageWidth-550)/2,stage.stageHeight/2-middle_right.height/2); positionMc(bottom_right,stage.stageWidth-bottom_right.width-20-(stage.stageWidth-550)/2,stage.stageHeight-bottom_right.height-20); }
10. Finally we will add the function that will toggle the fullscreen mode.
function fullScreenUP(event:MouseEvent):void { if (screenCheck == false) { stage.displayState = StageDisplayState.FULL_SCREEN; screenCheck = true; } else { stage.displayState = StageDisplayState.NORMAL; screenCheck = false; } } |