Home » ActionScript 3.0, Adobe, Flash AS 3

How to share properties between SWF files in Flash and ActionScript 3.0

22 August 2009 No Comment

When Actionscript 3.0 was a dream and all Flash developers worked with AS2 the _global property was a smart and fast way to share variables between swf files.

A classic scenario is the following:

1) In the main swf file there is the language selection and users can make their choice selecting the site language

2) This swf probably will have a menu to load different pages, each one with their content and galleries to load.

3) Every loaded page will need to know the selected language value to load the right content (for example calling xml files from an the /ENG folder if English was the selected language)

But to make it as simple as possible I’ll make you an easier sample, where you can set an userName property on the Main.fla and get the saved value from a loaded page (page.fla))

The final result (click on LOAD SWF):

This movie requires Flash Player 10

So, how can we share the userName property between SWF files?
There could be many ways to do it, but trying to follow an OOP approach, my idea is create a UserSettings Class with static getters and setters to contains all the user information.

package com.fabiobiondi.vo
{
 
    public class UserSettings
    {
 
       private  static var _userName:String ;
 
       /**
	   * Constructor
	   */
	   public function UserSettings(){}
 
	   /**
	   * UserName
	   */
       public static function set userName(val:String):void
       {
            _userName = val;
       }
 
       public static function get userName():String
       {
            return _userName;
       }
 
    }
 
}

THE MAIN FILE
So before load a new page (clicking on Button Component), we’ll save the userName value (taken from a TextInput Component) simply doing this:

UserSettings.userName = userNameTxt.text;

Following the Main.as document class (associated to main.fla)

package com.fabiobiondi.core {
 
	import com.fabiobiondi.vo.UserSettings;
 
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.ErrorEvent;
	import flash.net.URLRequest;
 
	import fl.controls.Button;
	import fl.controls.TextInput;
	import flash.text.TextFormat;
 
	public class Main extends MovieClip {
 
		private var loader:Loader;
		private var userNameTxt:TextInput;
		private var loadSwfBtn:Button;
 
		/**
		* Constructor
		*/
		public function Main () {
			createUI();
		}
 
		/**
		* Creation graphic elements
		*/
		private function createUI(e:MouseEvent=null) {
 
			// Create LOAD SWF button
			// Remember to import assets dragging Button Component inside Flash Library
			loadSwfBtn = new Button();
			loadSwfBtn.label = "Load SWF"
			loadSwfBtn.x = 10;
			loadSwfBtn.y = 10;
			loadSwfBtn.addEventListener(MouseEvent.CLICK, loadSwf)
			addChild(loadSwfBtn)
 
			// Create TEXT INPUT button
			// Remember to import assets dragging TextInput Component inside Flash Library
			userNameTxt = new TextInput();
			userNameTxt.text = "Fabio Biondi"
			userNameTxt.width = 100;
			userNameTxt.height = 20;
			userNameTxt.move(120,12);
			addChild(userNameTxt)
 
		}
 
		/**
		* Load the SWF
		*/
		private function loadSwf(e:MouseEvent=null) {
 
			// Save the userName value before each loading
			// NOTE: This value will be taken from the loaded page (page.swf)
			UserSettings.userName = userNameTxt.text;
 
			// Remove Previous loaded swf
			try {
				loader.unload();
			} catch (e:Error) { trace("first Time") }
 
			// Load page.swf
			loader = addChild(new Loader()) as Loader;
			loader.load(new URLRequest("page.swf"));
 
		}		
 
	}
}

THE PAGE FILE
To get this value, everywhere, in other MovieClips or in other swf files we’ll just need to import the UserSettings class and read the property value.

import com.fabiobiondi.vo.UserSettings;
userNameTxt.text = "Username: " + UserSettings.userName;

Following the loaded page Document Class (associated to Page.fla)

package com.fabiobiondi.core {
 
	import com.fabiobiondi.vo.UserSettings;
 
	import flash.display.MovieClip;
	import fl.controls.Label;
 
	public class Page extends MovieClip {
 
		/**
		* Constructor
		*/
		public function Page () {
 
			// Create LABEL to show the userName value
			// Remember to import assets dragging Label Component inside Flash Library
			var userNameTxt:Label = new Label();
			userNameTxt.text = "Fabio Biondi"
			userNameTxt.width = 200;
			userNameTxt.height = 20;
			userNameTxt.move(40, 100)
			addChild(userNameTxt)
 
			// We get the user name saved in the Main file using the UserSettings class
			userNameTxt.text = "Username: " + UserSettings.userName;
 
		}
 
	}
}

Download Source Code (FL CS4)

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.