In the below example i used the document class in Flex. i.e script class access from the external actionscript file. The content in the flex application can be set at center position dynamically when the browser resized. Document class the .mxml and .as file for the main application at the same location under the src folder. It can be placed in other location too. The stage will be accessed after the function call applicationCompleted. So you can set the stage related functionality after the applicationCompleted function then only it can be set the stage releated functions otherwise it should be null and its relevant functionality cannot be set. Just see the below example
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%"
creationComplete="onInit()" applicationComplete="onAppComplete()">
<mx:Script source="TextWorks.as"/>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%"
creationComplete="onInit()" applicationComplete="onAppComplete()">
<mx:Script source="TextWorks.as"/>
</mx:Application>
In above example the script tag refer the actionscript class. The actionscript class contains the entire main application scripts. TextWorks actionscript class coding as follows
import flash.display.Sprite;
import flash.events.Event;
import mx.core.UIComponent;
private var _spr:Sprite;
private var _ui:UIComponent;
private function onInit():void
{
_ui = new UIComponent();
_spr = new Sprite();
_spr.graphics.clear();
_spr.graphics.beginFill(0xFF0000,0.8);
_spr.graphics.drawRect(0,0,200,200);
_spr.graphics.endFill();
_ui.addChild(_spr);
addChild(_ui);
}
private function onAppComplete():void
{
stage.addEventListener(Event.RESIZE,onResize);
_spr.x = (stage.stageWidth - _spr.width)/2;
_spr.y = (stage.stageHeight - _spr.height)/2;
_ui.width = stage.stageWidth;
_ui.height = stage.stageHeight;
}
private function onResize(evt:Event):void
{
_spr.x = (stage.stageWidth - _spr.width)/2;
_spr.y = (stage.stageHeight - _spr.height)/2;
_ui.width = stage.stageWidth;
_ui.height = stage.stageHeight;
}
Run this application and see the sprite content will be placed at center position dynamically when the browser resize.
No comments:
Post a Comment