Thursday, 21 August 2014

ActionScript 3.0 Class File for Loading SWF files

Use the below code to create a class file for loading SWF files. You can customize this code to suit your requirements.

package  {

            // importing classes
            import flash.display.Loader;
            import flash.display.MovieClip;
            import flash.text.TextField;
            import flash.text.TextFormat;
            // end of importing classes

            // create a class file named LoadSwf;
            public class LoadSwf extends MovieClip {

                        // variables and constants
                        // all variables are defined as private. _loginLoad and                                      _titleBack are references to
                        // movieclips created in Flash. In the Library right click                                    Properties and select
                        // Export for Actionscript checkbox. The name of the class is                              the datatype here
                        // (login_load and title_background).
                        // Other variables here are Loader instance, URLRequest,                                  TextField, TextFormat
                        // and percent Number variable
private var _loginLoad:login_load;
                        private var _titleBack:title_background;
                        private var _myLoader:Loader;
                        private var _myUrl:URLRequest;
                        private var _percent:Number;
                        private var _swfHeading:TextField;
                        private var _textFormat:TextFormat;
                        // closeBtn is a reference to movieclip created in Flash. This                              is defined as public
                        // as it has to be accessed from flash
                           public var closeBtn:close_btn;
                        // end of variables and constants

// constructer function LoadSwf. This function adds the listener          ADDED TO STAGE                                             
                        public function LoadSwf() {
                                    // constructor code
                                    addEventListener(Event.ADDED_TO_STAGE,                                                 addedToStage);
                        // addedToStage function. Adds the Title Background Mc on                              the stage. This is
                        // just a movieclip with some background in it. Initially it is                              made invisible
                        // textField object is also created with textFormatting and                                similarly close
                        // button is added to the stage. Also we add an event                                      listener to the close
                        // button. A _loginLoad Movieclip is added. This is the                                      MovieClip which loads
                        // the swf files                        
                        public function addedToStage(evt:Event):void
                                    removeEventListener(Event.ADDED_TO_STAGE,                                           addedToStage);
                                    // Title Background Mc
                                    _titleBack = new title_background;
                                    _titleBack.visible = false;
                                    // TextFormat
                                    _textFormat = new TextFormat("Arial", 17,                                                   0xCCCCCC, true);
                                    // SWF Heading TF
                                    _swfHeading = new TextField();
                                    _swfHeading.visible = false;
                                    _swfHeading.selectable = false;
                                    _swfHeading.width = stage.stageWidth;
                                    _swfHeading.defaultTextFormat = _textFormat;
                                    // Close Button
                                    closeBtn = new close_btn;
                                    closeBtn.visible = false;
                                    closeBtn.addEventListener(MouseEvent.CLICK,                                             onCloseClick);
                                    // Login Load Mc
                                    _loginLoad = new login_load();
                                    _loginLoad.visible = false;

                        // start loading the swf file. This function will be called from                            the FLA file.
                        // It accepts certain parameters like url, heading, width,                                  height and y location
                        // You can make changes to these as per your requirements.                             Within the function
                        // we reveal all the movieclips, set their locations and load                               the SWF using the loader and urlrequest
                        public function startLoad(url:String, heading:String,                                       swfWidth:int, swfHeight:int, swfY:int):void
                                    // SWF Heading TF
                                    _swfHeading.visible = true;
                                    _swfHeading.text = heading;
                                    _swfHeading.x = (stage.x) + 10;
                                    // Close Button
                                    closeBtn.visible = true;
                                    closeBtn.x = (stage.stageWidth) - 75;
                                    // Title Background Mc
                                    _titleBack.visible = true;
                                    _titleBack.x = (stage.stageWidth/2)-                                                         (_titleBack.width/2);
                                    _titleBack.y = (stage.stageHeight / 2) -                                                     (_titleBack.height / 2);
                                    // Login Load Mc
                                    _loginLoad.visible = true;
                                    // Loader
                                    _myLoader = new Loader();
                                    _loginLoad.width = swfWidth;
                                    _loginLoad.height = swfHeight;
                                    _loginLoad.y = swfY;
                                    _myUrl = new URLRequest(url);
                                    _myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
                                    _myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);

                        // on load complete handler
                        private function onCompleteHandler(loadEvent:Event):void
                                    // Loading Completed
                        // on load progress handler. This calculates the loading                                    percentage
                        // which can be used as a preloader
                        private function onProgressHandler(mProgress:ProgressEvent):void
                                    _percent = mProgress.bytesLoaded /                                                                           mProgress.bytesTotal;
                        // on clicking the close button this function is executed. It                                hides all the
                        // movieclips, unloads the loader and stops all the sounds in                            case the sound is playing in the movie
                        private function onCloseClick(evt:MouseEvent):void
                                    // Title Background Mc
                                    _titleBack.visible = false;
                                    // Login Load Mc
                                    _loginLoad.visible = false;
                                    // Close Button
                                    closeBtn.visible = false;
                                    // SWF Heading TF
                                    _swfHeading.visible = false;
                                    // Unload the _myLoader Loader
                                    // Remove the _loginLoad movieclip from the stage
                                    // Stop all the sounds from playing

Save this class file in a folder. You can give any name to the folder. But the industry practice it to name it in reverse order of your website. For example if your website name is
Create a folder com and within com create a folder tcsion. So the location will be com/tcsion.

It’s very easy to use this in Flash. Create a new document in Flash and save the file. The file should be saved outside the com folder. In the ActionScript settings specify the source path as ./com/tcsion or as you have defined your folder structure. Below is the screenshot for your reference.

Advanced ActionScript 3.0 settings

After defining the source path you can start adding the script in the timeline.
Initially in the 1st Keyframe add the below script

// import the class
import LoadSwf;

// create a new instance and add it to the display list
var swfLoad:LoadSwf = new LoadSwf();
// Now the loading functionality is ready to be used. This can be used either on button click or // on Enterframe.
someBtn.addEventListener(MouseEvent.CLICK, onClk);
function onClk(evt:MouseEvent):void
            MovieClip(root).swfLoad.startLoad("swf/Demo_for_Purchase_Requisition.swf", "Demo for Purchase Requisition", 840, 510, 30);

No comments:

Post a Comment