1. Code
  2. Coding Fundamentals
  3. Game Development

Create a Dynamic Deep Linking Flash Web Site with SWFAddress

Scroll to top

In this tut, we will be exploring the wonder that is SWFAddress, discussing why your site should have deep linking, and the processes involved in implementing deep linking in your Flash projects.

This website is XML driven. Also we shall be looking at SWFAddress's built in functions and how they are used to create the final result. Enjoy!

Check out the demo above in a new window. Notice how your browser's URL changes as you navigate through the mini-site.


Step 1: Why use Deep Linking?

Deep linking is a great way to structure a website and also is great way to structure code.

Deep linking is basically giving the user a chance to link to a certain area of a Flash movie through a simple URL.

For example if you wanted to show a portfolio to a client and you gave them the link to a Flash website without deep linking. Well their guess is as good as yours where to find that content within the SWF. But with deep linking you can link to any content in a Flash movie -- for example: website.com/#/portfolio/section_3. This may seem a complex task but it isn't with SWFAddress deep linking.

Deep linking has other benefits (for example, SEO) and it basically turns a boring Flash movie into an expandable application.


Step 2: Getting Prepared

You will need for this tut: Caurina Tweener, SWFAddress for AS3, SWFObject.

You'll also need main.html from the Source download.


Step 3: Import, #include, what??

If you have previous experience with swfaddress, you may have come in contact with this issue. You have tried to use the import directive to use swfaddress or if that failed you tried #include 'SWFAddress.as'. Well the truth is you don't have to do anything at all.

All you need to do is put the SWFAddress.as file where your swf is and swfaddress is automatically available for actionscript.


Step 4: File Structure

Create an empty main.fla. (I would normally write code in external as files but I think all the code in one place is better understood).

From the SWFaddress download get the swfobject and swfaddress folders and place them in the main folder. If you are having trouble finding these just download them from the source at the top of the page.

And of course get the SWFAddress.as and SWFAddressEvent.as and place them in the main folder. They are also in the Source download if you are having trouble finding them.

Download the source and copy main.html to the main folder.


Step 5: Create Four SWFs

Create 4 dummy SWFs; for example, SWFs with some random text in them, each with a different title and color of font. There are four random ones included in the Source download.


Step 6: Create the XML File

1
2
<?xml version="1.0" encoding="UTF-8"?>
3
<xml>
4
	<menu>
5
		<item item_name = "Home" item_module = "home.swf" />
6
		<item item_name = "About" item_module = "about.swf" />
7
		<item item_name = "Portfolio" item_module = "portfolio.swf" />
8
		<item item_name = "Contact" item_module = "contact.swf" />
9
	</menu>
10
</xml>

Call it main.xml. We will use xml to store our data. Now we have created the xml file we will start the good stuff, coding...


Step 7: Create the Menu Array

Below is the code required to load the xml menu in Flash. The comments in the code guide you through the process.

Go to frame 1, Layer 1 in the main .fla file you created earlier.

Add this code. It's well-commented to help you understand what it does.

1
2
3
// set stage settings

4
5
var fileStage:Stage = this.stage;
6
fileStage.scaleMode =  StageScaleMode.NO_SCALE;
7
fileStage.align = StageAlign.TOP_LEFT;
8
9
// import caurina tweener

10
11
import caurina.transitions.Tweener;
12
13
// main timeline object

14
15
var index:Object = this;
16
17
// New Xml Object

18
19
var fileXml:XML;
20
21
// check how many menu items we have.

22
23
var totalMenuItems:Number;
24
25
// New URL Request - Add a COMPLETE event listener to load the site once the XML load has been completed.

26
27
var xmlLoader:URLLoader = new URLLoader(new URLRequest ("main.xml"));
28
xmlLoader.addEventListener(Event.COMPLETE, createSite);
29
30
function createSite (e:Event):void {
31
	
32
// store the xml file in our xml variable	

33
34
	fileXml = new XML(e.target.data);
35
	
36
// use a constructor function to use the xml 	

37
	
38
	index.createMainMenu(fileXml.menu);
39
	
40
} 
41
42
function createMainMenu(xmlData:XMLList):void {
43
44
	// create the length of our menu 

45
	
46
	index.totalMenuItems = xmlData.*.length();
47
	
48
	// create a movie clip with the instance name menu_clip and add it to the stage.

49
	
50
	// then use a <code>for</code> loop to create our menu

51
	
52
	for (var i:uint = 0; i < index.totalMenuItems; i++) {
53
	
54
		/*

55
		create a movieclip and export for actionscript with the class name: menuItem

56
		it should contain one dynamic textbox with the instance name of menu_item_text.

57
		*/
58
		
59
		// create our new menu item

60
		
61
		var menu_item:menuItem = new menuItem();
62
		
63
		// set the identification 

64
		
65
		menu_item.id = i;
66
		menu_item.name = "id" + i;    
67
		
68
		// position our menu items dynamically

69
		
70
		menu_item.x = (i * menu_item.width);
71
		
72
		// add the xml values to the text box inside the new item.

73
		
74
		menu_item.menu_item_text.text = xmlData.item.@item_name[i];
75
		
76
		// add the module address to the menu variable / It will come in useful later.

77
		
78
		menu_item.item_module = xmlData.item.@item_module[i];
79
		
80
		/*

81
		create a new layer and place the menu_clip movieclip onto that layer. 

82
		Give it an instance name of menu_clip

83
		*/
84
		
85
		// add each menu item to the menu_clip movieclip using addChild()

86
		
87
		index.menu_clip.addChild(menu_item);
88
	
89
	}
90
	
91
}

Step 8: Create Button Actions

This is what our site should look like now:

So let's continue by giving button events. The code below should be placed on line 78 on frame 1 actions layer just above the addchild method we used.

1
2
3
	// assign events to menu buttons. 

4
	
5
	// rollover & rollout effects 

6
	
7
	menu_item.addEventListener(MouseEvent.MOUSE_OVER, menuRollOver);
8
	menu_item.addEventListener(MouseEvent.MOUSE_OUT, menuRollOut);
9
	menu_item.addEventListener(MouseEvent.MOUSE_DOWN, menuPress);

Add the functions below to handle these events below the createMainMenu function:

1
2
3
function menuRollOver (e:MouseEvent) {
4
5
	// menu button variable - We use getChildByName to aim at the correct menu item

6
	
7
	// e.target.id is referring to the menu_item.id we defined earlier.

8
	
9
	var button:MovieClip = index.menu_clip.getChildByName("id" + e.target.id) as MovieClip;
10
	
11
	// set the animation usign caurina tweener

12
	
13
	Tweener.addTween( button, {alpha: 0.5, time: 2, transition:"easeOutExpo"});
14
15
}
16
17
function menuRollOut (e:MouseEvent) {
18
19
	// menu button variable - We use getChildByName to aim at the correct menu item

20
	
21
	// e.target.id is referring to the menu_item.id we defined earlier.

22
	
23
	var button:MovieClip = index.menu_clip.getChildByName("id" + e.target.id) as MovieClip;
24
	
25
	// set the animation using caurina tweener

26
	
27
	Tweener.addTween( button, {alpha: 1, time: 2, transition:"easeOutExpo"});
28
29
}
30
31
function menuPress (e:MouseEvent) {
32
	
33
	// SWFAddress set value function

34
35
	SWFAddress.setValue(e.target.deep_link);
36
37
}

Step 9: SWFAddress on Change Function

This is the main event that handles all the swfaddress changes. It is where we will apply our application logic in order for our site to work. Let's have a look at the main function in detail.

1
2
3
function handleSWFAddress(e:SWFAddressEvent) {
4
5
	/*

6
	set html title names

7
	*/
8
9
	var htmlTitle:String= "XML Deep linking Tutorial";
10
	for (var i:Number = 0; i < e.pathNames.length; i++) {
11
		htmlTitle+=' / '+String(e.pathNames[i].substr(0,1).toUpperCase()+e.pathNames[i].substr(1)).split("_").join(" ");
12
	}
13
	
14
	/*

15
	

16
	To format the HTML title we use the split and join techniques. 

17
	These replace _ with a space. We make it a string to ensure it is useable, like so:

18
	

19
	String(e.pathNames[i].substr(0,1).toUpperCase()+e.pathNames[i].substr(1)).split("_").join(" ");

20
	

21
	*/
22
	
23
	// this function does all the work and  assigns the HTML title

24
	
25
	SWFAddress.setTitle(htmlTitle);
26
27
}
28
29
// when the SWFAddress.setValue() is fired this event listens for it and inits the function above.

30
31
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleSWFAddress);

Step 10: setValue() Explanation

SWFAddress.setValue("somevalue");

When this event is fired -- for example when someone presses the button -- the swfaddress value is set and triggers the onChange() listener. Therefore in order to create our modules to load we need to create some code inside the swfAddress onChange() listener.


Step 11: Events Function

Now we are going back up to our menu for loop. We need to add an additonal variable that contains a function:

1
2
3
// Place this code in our <code>for</code> loop below the addEventListeners

4
5
		/*

6
		this variable contains a function that will contain the events when the swfaddress in changed.

7
		*/
8
		
9
		menu_item.init = function () {
10
			
11
			/*

12
			create a new movieclip and call it module_clip, then place it on a new layer then give it

13
			the instance name of module_clip

14
			*/
15
			
16
			// new variable that has contains the path of the swf we are loading in.

17
			
18
			var module:String = this.item_module;
19
			
20
			// fade out our current module if any, then commence the load of our new module using the tweener's onComplete function

21
			
22
			Tweener.addTween( index.module_clip, {alpha: 0, time: 2, transition:"easeOutExpo", onComplete:function () 
23
				{
24
				
25
					index.createModule(module);
26
					
27
					// remove the previous module only if it isn't the first load

28
					
29
					if (index.firstLoad == false) {
30
					
31
						index.module_clip.removeChildAt(0);
32
					
33
					}            
34
				
35
				}
36
			});
37
			
38
		}

Step 12: Creating the Module Function

Before we look at the swfaddress events, we will create the module to load the module in. It is complete with I/O error handling. It is good practice to handle errors when you can.

1
2
3
// At the top of the actionscript place this:

4
5
var firstLoad:Boolean = true; // prevents null errors

This is the module function. The comments guide you through the process.

1
2
function createModule (url:String) {
3
	
4
	// we create a new loader

5
6
    var loader:Loader = new Loader();
7
	
8
	// we create a new url request for our loader

9
	
10
    var moduleURL:URLRequest = new URLRequest(url);
11
	
12
	// we add event listeners to the loader. some are not necessary but I included them for convenience

13
	
14
    loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
15
	
16
	// when the module has loaded we trigger loadComplete() with a event listener.

17
	
18
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
19
    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadError);
20
    loader.load(moduleURL);
21
    
22
    function loadProgress (e:ProgressEvent) {
23
                
24
    }
25
	
26
	// triggered when module has loaded

27
    
28
    function loadComplete (e:Event) {
29
                
30
		// fade in the module

31
		
32
		Tweener.addTween( index.module_clip, {alpha: 1, time: 2, transition:"easeOutExpo"});
33
		
34
		// add the current module to the module_clip movieclip by using addChild

35
		
36
		index.module_clip.addChild(e.currentTarget.content);
37
38
		// now we have the first load we set firstLoad to false. It then remains false for the whole session.

39
		
40
		index.firstLoad = false;
41
42
    }
43
            
44
    function loadError (e:Event) {
45
    
46
        trace("error");
47
    
48
    }
49
}

Step 13: Summary

To recap: We have created the menu, created the function that will occur when SWFAddress.setValue() is triggered but we still need to create our application logic. What I mean by application logic is how we are going to use swfAddress to load in certain parts of our application.


Step 14: Creating the Deep Link

Let's go back to our menu for loop and define the deep link there. Below where we set the menu_item.name place this code. This code creates the deep link value:

1
2
// set the menu item deep link plus formatting with split and join. toLowerCase() makes every character lowercase. 

3
menu_item.deep_link = xmlData.item.@item_name[i].split(" ").join("_").split("/").join("_").toLowerCase();

Its use will become clear very soon.


Step 15: Application Logic

Now let's go back to our function that handles swfaddress events: handleSWFAddress

We now need a certain part of code that will load the correct module. The code below shows you how to apply the correct logic in order for the application to load the correct module. The comments guide you through the process. The new part of this function starts at the creating the for loop.

1
// function to handle swfaddress events.

2
function handleSWFAddress(e:SWFAddressEvent) {
3
4
	/*

5
	set html title names

6
	*/
7
8
	var htmlTitle:String= "XML Deep linking Tutorial";
9
	for (var i:Number = 0; i < e.pathNames.length; i++) {
10
		htmlTitle+=' / '+String(e.pathNames[i].substr(0,1).toUpperCase()+e.pathNames[i].substr(1)).split("_").join(" ");
11
	}
12
	
13
	/*

14
	

15
	To format the HTML title we use the split() and join() techniques. 

16
	These replace _ with a space. We make it a string to ensure it is useable, like so:

17
	String(e.pathNames[i].substr(0,1).toUpperCase()+e.pathNames[i].substr(1)).split("_").join(" ");

18
	

19
	*/
20
	
21
	// this function does all the work and  assigns the HTML title

22
	
23
	SWFAddress.setTitle(htmlTitle);
24
	
25
	// create a <code>for</code> loop to iterate through the total number of menu items. use index.totalMenuitems to do so.

26
	
27
	/* 

28
	You may be wondering why I am using n instead of i in the <code>for</code> loop. Well if there are two <code>for</code> loops in the same function

29
	and both loops are iterating through the variable i it throws an error. Using n or another var solves this error.

30
	*/
31
	
32
	for (var n:uint = 0;  n < index.totalMenuItems; n++) {
33
		
34
		// this var is used to iterate through all our menu items

35
	
36
		var button:MovieClip = index.menu_clip.getChildByName("id"+ n) as MovieClip;
37
38
		// the if statement below is the most important part of our code. 

39
		
40
		// SWFAddress.getValue() is explained in the next step

41
		
42
		// we need to get rid of the "/" on the front in order for it to equal the button's deep link

43
		
44
		if (button.deep_link == SWFAddress.getValue().split("/")[1]) {
45
			
46
			// if any of button deep links equal the URL set then it initiates the menu_item.init function we set earlier

47
48
			button.init();
49
			
50
			// to stop the code we use return.

51
			
52
			return;
53
			
54
		}
55
	}
56
}
57
58
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleSWFAddress);

Step 16: SWFAddress.getValue()

SWFAddress.getValue() is a great way to test your deep linking site without web server. The best way to test your deep linking using SWFAddress.getValue() is to use the trace function. For example:

1
2
trace(SWFAddress.getValue());

This will trace the current swfAddress value in the output.


Step 17: Loading the First Menu Item

Why on earth wouldn't you want to load the first menu item? There's no good reason, therefore I will show you how to do it. Go to the handleSWFAddress() function and drag it into the createMainMenu() function below all the previous code. Add this code below the second for loop (the one we just created):

1
2
// this var is referenced to the first menu item

3
var firstButton:MovieClip = index.menu_clip.getChildByName("id" + 0) as MovieClip;
4
5
// use the dispatch event to fire the press event on the first button. This then sets the deep link. The swfAddress on change listener function inits.

6
// Therefore the <code>for</code> loop above will load the first module.

7
firstButton.dispatchEvent( new MouseEvent(MouseEvent.MOUSE_DOWN));

Step 18: Extra Enhancements

We are now going to apply validation to the menu as gift for reading this tut. :)

Go up to the menu_item.init function in the menu for loop.

1
2
for (var i:uint = 0; i < index.totalMenuItems; i++) {
3
	
4
	var button:MovieClip = index.menu_clip.getChildByName("id" + i) as MovieClip;
5
	
6
	if( button != this ) {
7
		
8
		button.enabled = true;
9
		button.addEventListener(MouseEvent.MOUSE_OUT, menuRollOut);
10
		button.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT));
11
		
12
	} else {
13
		
14
		button.enabled = false;
15
		button.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER));
16
		button.removeEventListener(MouseEvent.MOUSE_OUT, menuRollOut);
17
		
18
	}
19
}

Step 19: Troubleshooting

OK! Now we should have a working project. But don't throw a party just yet. Check the following things:

  • You have completed every step.
  • DO NOT PUBLISH the SWF, just export the movie otherwise you will override the main.html document you obtained from the Source download.
  • Make sure you have imported all the necessary imports. Well all the imports are necessary without just one the whole thing would not function properly.
  • Make sure the handleSWFAddress() function is in the createMainMenu() function.

Step 20: What does main.html do?

Let's take a look at main.html:

1
2
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
    <head>
4
        <title></title>
5
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
        <style type="text/css">
7
        /*<![CDATA[*/
8
            html, body, #website {
9
                height: 100%;
10
                overflow: hidden;
11
            }
12
            body {
13
                background: #f7f7f7;
14
                font: 86% Arial, "Helvetica Neue", sans-serif;
15
                margin: 0;                
16
            }
17
        /*]]>*/
18
        </style>
19
        <script type="text/javascript" src="swfobject/swfobject.js"></script>
20
        <script type="text/javascript" src="swfaddress/swfaddress.js"></script>
21
        <script type="text/javascript">
22
        /*<![CDATA[*/
23
            swfobject.embedSWF('main.swf', 'website', '100%', '100%', '9', 
24
                'swfobject/expressinstall.swf', {domain: '*'}, {allowscriptaccess: 'always', allowfullscreen: 'true', bgcolor: '#f7f7f7', menu: 'false', wmode: 'opaque'}, {id: 'website'});
25
        /*]]>*/
26
        </script>
27
    </head>
28
    <body>
29
        <div id="website">
30
            <p>In order to view this page you need Flash Player 9+ support!</p>
31
            <p>
32
                <a href="http://www.adobe.com/go/getflashplayer">
33
                    <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
34
                </a>
35
            </p>
36
        </div>
37
    </body>
38
</html>

A lot of that is just styling the file, changing the background color, positioning the SWF, things like that. Here are the key lines:

1
2
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
    <head>
4
        <script type="text/javascript" src="swfobject/swfobject.js"></script>
5
        <script type="text/javascript" src="swfaddress/swfaddress.js"></script>
6
        <script type="text/javascript">
7
        /*<![CDATA[*/
8
            swfobject.embedSWF('main.swf', 'website', '100%', '100%', '9', 
9
                'swfobject/expressinstall.swf', {domain: '*'}, {allowscriptaccess: 'always', allowfullscreen: 'true', bgcolor: '#f7f7f7', menu: 'false', wmode: 'opaque'}, {id: 'website'});
10
        /*]]>*/
11
        </script>
12
    </head>
13
    <body>
14
        <div id="website">
15
        </div>
16
    </body>
17
</html>

This includes the SWFObject and SWFAddress JavaScript files, and uses SWFObject to load main.swf in the div element called "website". That's all there is to it! Check out this screencast for an intro to SWFObject.


Step 20: The Whole Code Commented

Below is the whole code commented and working.

1
2
3
// set stage settings

4
5
var fileStage:Stage=this.stage;
6
fileStage.scaleMode=StageScaleMode.NO_SCALE;
7
fileStage.align=StageAlign.TOP_LEFT;
8
9
// import caurina tweener

10
11
import caurina.transitions.Tweener;
12
13
// main timeline object

14
15
var index:Object=this;
16
17
// New Xml Object

18
19
var fileXml:XML;
20
21
// check how many menu items we have.

22
23
var totalMenuItems:Number;
24
25
// prevents null errors

26
27
var firstLoad:Boolean = true;
28
29
// New URL Request - Add a complete event listener to load the site once the load has been completed.

30
31
var xmlLoader:URLLoader = new URLLoader();
32
xmlLoader.load (new URLRequest("main.xml"));
33
xmlLoader.addEventListener( Event.COMPLETE, createSite);
34
35
function createSite(e:Event) {
36
    
37
    // store the xml file in our xml variable

38
    
39
    fileXml=new XML(e.target.data);
40
    
41
    // use a constructor function to use the xml

42
    
43
    index.createMainMenu(fileXml.menu);
44
    
45
}
46
47
function createMainMenu(xmlData:XMLList) {
48
    
49
    // create the length of our menu

50
    
51
    index.totalMenuItems=xmlData.*.length();
52
    
53
    // create a movie clip with the instance name menu_clip and add it to the stage.

54
    
55
    // then use a <code>for</code> loop to create our menu

56
    
57
    for (var i:uint = 0; i < index.totalMenuItems; i++) {
58
        
59
        /*

60
        create a movieclip and export for actionscript with the class name: menuItem

61
        it should contain one dynamic textbox with the instance name of menu_item_text.

62
        */
63
        
64
        // create our new menu item

65
        
66
        var menu_item:menuItem = new menuItem();
67
        
68
        // set the identification

69
        
70
        menu_item.name="id"+i;
71
        menu_item.id=i;
72
        
73
        // set the menu item deep link plus formatting with split and join. toLowerCase() makes every character lowercase

74
        
75
        menu_item.deep_link = xmlData.item.@item_name[i].split(" ").join("_").split("/").join("_").toLowerCase();
76
        
77
        // give it a button cursor & make the target the button and not its children

78
        
79
        menu_item.buttonMode=true;
80
        menu_item.mouseChildren=false;
81
        
82
        // position our menu items dynamically

83
        
84
        menu_item.x = (i * menu_item.width);
85
        
86
        // add the xml values to the text box inside the new item.

87
        
88
        menu_item.menu_item_text.text=xmlData.item.@item_name[i];
89
        
90
        // add the module address to the menu variable / It will come in useful later.

91
        
92
        menu_item.item_module=xmlData.item.@item_module[i];
93
        
94
        /*

95
        create a new layer and place the menu_clip movieclip onto that layer.

96
        Give it an instance name of menu_clip

97
        */
98
        
99
        // assign events to menu buttons. place the functions to handle these events below the createMainMenu function

100
        
101
        // rollover & rollout effects

102
        
103
        menu_item.addEventListener(MouseEvent.MOUSE_OVER, menuRollOver);
104
        menu_item.addEventListener(MouseEvent.MOUSE_OUT, menuRollOut);
105
        menu_item.addEventListener(MouseEvent.MOUSE_DOWN, menuPress);
106
        
107
        /*

108
        this variable contains a function that will contain the events when the swfaddress in changed.

109
        */
110
        
111
        menu_item.init = function () {
112
            
113
            /*

114
            create a new movieclip and call it module_clip, then place it on a new layer then give it

115
            the instance name of module_clip

116
            */
117
            
118
            // new variable that has contains the path of the swf we are loading in.

119
            
120
            var module:String = this.item_module;
121
            
122
            // fade out our current module if any, then commence the load of our new module using the tweener's onComplete function

123
            
124
            Tweener.addTween( index.module_clip, {alpha: 0, time: 2, transition:"easeOutExpo", onComplete:function () {
125
                    
126
                    index.createModule(module);
127
                    
128
                    if (index.firstLoad == false) {
129
                        
130
                        index.module_clip.removeChildAt(0);
131
                        
132
                    }
133
                    
134
            }});
135
            
136
            for (var i:uint = 0; i < index.totalMenuItems; i++) {
137
                
138
                var button:MovieClip = index.menu_clip.getChildByName("id" + i) as MovieClip;
139
                
140
                if( button != this ) {
141
                    
142
                    button.enabled = true;
143
                    button.addEventListener(MouseEvent.MOUSE_OUT, menuRollOut);
144
                    button.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT));
145
                    
146
                    } else {
147
                    
148
                    button.enabled = false;
149
                    button.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER));
150
                    button.removeEventListener(MouseEvent.MOUSE_OUT, menuRollOut);
151
                    
152
                }
153
            }
154
        }
155
        
156
        // add each menu item to the menu_clip movieclip using addChild()

157
        
158
        index.menu_clip.addChild(menu_item);
159
        
160
    }
161
    
162
    // function to handle swfaddress events.

163
    
164
    function handleSWFAddress(e:SWFAddressEvent) {
165
        
166
        /*

167
        set html title names

168
        */
169
        
170
        var htmlTitle:String= "XML Deep linking Tutorial";
171
        for (var i:Number = 0; i < e.pathNames.length; i++) {
172
            htmlTitle+=' / '+String(e.pathNames[i].substr(0,1).toUpperCase()+e.pathNames[i].substr(1)).split("_").join(" ");
173
        }
174
        
175
        /*

176
        

177
        to format the html title we use the split and join technique. It replaces _ with a space. We make it a string to ensure it is useable.

178
        

179
        String(e.pathNames[i].substr(0,1).toUpperCase()+e.pathNames[i].substr(1)).split("_").join(" ");

180
        

181
        */
182
        
183
        // this function does all the work and assigns the html title

184
        
185
        SWFAddress.setTitle(htmlTitle);
186
        
187
        // create a <code>for</code> loop to iterate through the total number fo menu items. hence we use index.totalMenuitems to do so.

188
        
189
        /*

190
        You may be wondering why I am using n instead of i in the <code>for</code> loop. Well Adobe failed to realise that if there are two <code>for</code> loops in the same function

191
        and both loops are iterating through the variable i it throws an error. Using n or another var solves this error.

192
        */
193
        
194
        
195
        for (var n:uint = 0; n < index.totalMenuItems; n++) {
196
            
197
            // this var is used to iterate through all our menu items

198
            
199
            // this var is referenced to the first menu item

200
            
201
            var button:MovieClip = index.menu_clip.getChildByName("id"+ n) as MovieClip;
202
            
203
            // the if statement below is the most important part of our code.

204
            
205
            // SWFAddress.getValue() is explained in the next step

206
            
207
            // we need to get rid of the "/" on the front in order for it to equal the button's deep link

208
            
209
            if (button.deep_link == SWFAddress.getValue().split("/")[1]) {
210
                
211
                // if any of button deep links equal the URL set then it initiates the menu_item.init function we set earlier

212
                
213
                button.init();
214
                
215
                // to stop the code we use return.

216
                
217
                return;
218
                
219
            }
220
        }
221
        
222
        var firstButton:MovieClip = index.menu_clip.getChildByName("id" + 0) as MovieClip;
223
        
224
        // use the dispatch event to fire the press event on the first button. This then sets the deep link.

225
        
226
        // Therefore the <code>for</code> loop above will load the first module.

227
        
228
        firstButton.dispatchEvent( new MouseEvent(MouseEvent.MOUSE_DOWN));
229
        
230
    }
231
    
232
    SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleSWFAddress);
233
    
234
}
235
236
function menuRollOver(e:MouseEvent) {
237
    
238
    // menu button variable - We use getChildByName to aim at the correct menu item

239
    
240
    // e.target.id is referring to the menu_item.id we defined earlier.

241
    
242
    var button:MovieClip=index.menu_clip.getChildByName("id"+e.target.id) as MovieClip;
243
    
244
    // set the animation using caurina tweener

245
    
246
    Tweener.addTween( button, {alpha: 0.5, time: 2, transition:"easeOutExpo"});
247
    
248
}
249
250
function menuRollOut(e:MouseEvent) {
251
    
252
    // menu button variable - We use getChildByName to aim at the correct menu item

253
    
254
    // e.target.id is referring to the menu_item.id we defined earlier.

255
    
256
    var button:MovieClip=index.menu_clip.getChildByName("id"+e.target.id) as MovieClip;
257
    
258
    // set the animation using caurina tweener

259
    
260
    Tweener.addTween( button, {alpha: 1, time: 2, transition:"easeOutExpo"});
261
    
262
}
263
264
function menuPress(e:MouseEvent) {
265
    
266
    // SWFAddress set value function

267
    
268
    SWFAddress.setValue(e.target.deep_link);
269
    
270
}
271
272
273
/*

274
this is the function that loads the selected module

275
*/
276
277
function createModule (url:String) {
278
    
279
    // we create a new loader

280
    
281
    var loader:Loader = new Loader();
282
    
283
    // we create a new url request for our loader

284
    
285
    var moduleURL:URLRequest = new URLRequest(url);
286
    
287
    // we add event listeners to the loader. some are not necessary but I included them for convenience

288
    
289
    loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
290
    
291
    // when the module has loaded we trigger the complete with a event listener.

292
    
293
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
294
    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadError);
295
    loader.load(moduleURL);
296
    
297
    function loadProgress (e:ProgressEvent) {
298
        
299
    }
300
    
301
    // triggered when module has loaded

302
    
303
    function loadComplete (e:Event) {
304
        
305
        // fade in the module

306
        
307
        Tweener.addTween( index.module_clip, {alpha: 1, time: 2, transition:"easeOutExpo"});
308
        
309
        // add the current module to the module_clip movieclip by using addChild

310
        
311
        index.module_clip.addChild(e.currentTarget.content);
312
        
313
        // now we have the first load we set firstLoad to false

314
        
315
        index.firstLoad = false;
316
        
317
    }
318
    
319
    function loadError (e:Event) {
320
        
321
        trace("error");
322
        
323
    }
324
}

Conclusion

Thanks for reading this tut. If you have any questions or problems leave them below. By now you should understand how to build a deep linking website without needing to understand how a dynamic website should be coded.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.