// you’re reading...

Programming tips

Flash Tip: Deep linking with SWFAddress

alt text

Flash man by Zerofractal Studio

7/22/09 Update: Read a user’s feedback on my sample files, and download his more simplified and updated files HERE.

4/9/09 Update: I just found this great video tutorial on SWFAddress by Flash guru Lee Brimelow. It’s definitely worth a look!

2/25/09 Update: I have provided example files and tips in an updated post. Please refer to both posts for help troubleshooting bugs. Enjoy!

Ever been annoyed when someone clicks the “back” button while viewing a Flash presentation? Your initial reaction is, “You can’t do that in Flash!” Or, even more frustrating, you want to show someone a cool component within a Flash project, but it’s too confusing to say, “Go to this URL, then click “Videos,” then click “My video.” etc etc? Actually, neither of these quirks should arise anymore. Let me introduce you to SWFAddress and SWFObject, which enable the back/forward buttons of your browser AND allow for deep linking.

For this tutorial, I will tell you how I taught myself, and hopefully it will help others …

1) To get a grasp of how this all works, start with this great post on draw.logic, “Deep Linking in Flash – SWFObject and SWFAddress.” After reading it, I felt like SWFAddress was a more robust application than SWFObject, so I stuck with SWFAddress (if I am incorrect in saying this, someone please correct me). However, if you want more information on SWFObject, read this post on deconcept, as well as it’s documentation on google code.

2) Download SWFAddress here. At time of publication, it will be SWFAddress 2.1. If you are the type of person that likes to see examples of it working before you get lost in code, here is their example in Flash.

3) Unzip the folder and open it. There are sample files and code for Flash, Flex, AJAX, and several others. For Flash, if you are coding with AS1, use the “Flash” folder. For AS2, use the “Adobe” folder, and for AS3, use the “CS3″ folder.
NOTE: For my tutorial I will be using AS2.

4) Open up website.fla, SWFAddress.as and SWFAddressEvent.as … take a look around. As you can see, these files work by using keyframe coding, so if you are coding with OOP and only using one frame, you’ll need to find another tutorial. (But good for you for using good coding practices!)

5) Start customizing this code for your website. (NOTE: Save your .fla right next to their website.fla so it can read the two class files and javascript file.) For mine, I had a preloader on frame 1, an intro on frame 2, and my main content starting on frame 3. Below is sample code, from Asual, with my comments:

Frame 2 (with no label) – used in this example as an intro:
// Custom utilities
function replace(str, find, replace) {
return str.split(find).join(replace);
}
function toTitleCase(str) {
return str.substr(0,1).toUpperCase() + str.substr(1);
}
function formatTitle(title) {
//change 'SWFAddress Website' to your title/heading name
return 'SWFAddress Website' + (title != '' ? ' / ' + toTitleCase(replace(title, '/', ' / ')) : '');
}

// SWFAddress handling
SWFAddress.setStrict(false);
SWFAddress.onChange = function() {
var value = SWFAddress.getValue();
var path = SWFAddress.getPath();
var id = SWFAddress.getParameter('id');
if (_currentframe == 2 && value == '') {
intro.gotoAndPlay(2); //intro is the instance name of our intro movieclip
} else {
gotoAndStop('$' + value);
}
trace (path);
SWFAddress.setTitle(formatTitle(path + (id != '' ? '/' + id : '')));
}
stop();

/*If you do not have an intro, you can change what is inside the SWFAddress.onChange function to match your needs (probably by taking out the following if statement: if (_currentframe == 2 && value == ”){ …)
*/

Frame 3 – used in this example as home (with a frame label of $):
stop();
SWFAddress.setValue('');
about.onRelease = function() {
SWFAddress.setValue('about');
}
about.onRollOver = function() {
SWFAddress.setStatus('about');
}
about.onRollOut = function() {
SWFAddress.resetStatus();
}

/* “about” is a button linking to the about page … copy the onRelease, onRollOver, and onRollOut code and use for every button that you want to enable deep linking*/

6) Add Labels:
For each section that you want to deep link, add appropriate label tags. For instance, our about page would have a label of $about … Look at how they did their labels in website.fla if you are confused. NOTE: Do not put a frame label on your intro page.

7) Publish your files, creating an .index.html and a .swf file

8 ) You are now going to activate the javascript file, located in the “swfaddress” folder, named “swfaddress.js” Open your html file, and place the following code beneath the title and meta information (right next to the javascript call code for AC_RunActiveContent.js):

9) FTP your .swf, index.html, swfaddress folder, and AC_RunActiveContent.js file to your desired location. Call up your URL, and hopefully it all should work. You can now use the back and forward buttons and your deep links should work. YAY!

Let me know if you have any questions or if it’s not working for you.

Good luck and happy coding!

Other posts that might interest you:

Discussion

13 comments for “Flash Tip: Deep linking with SWFAddress”

  1. Hey thanks for this, It did help alot – I am currently trying to embed this badboy with SWFobject and it does not seem to like each other – but hey I will let you know what i find – thanks for the post!

    Posted by AudioVoyeur | 18 October 2008, 7:26 AM
  2. Dear Tracy,

    Thank you for this great tutorial.

    I must be doing something wrong, but it does not work for me.
    The site I am working on is as follows: preloader on the first frame, on the second frame – “navigation” movie clip with buttons inside, and “thepages” movie clip with the contence. I have put the main code on this second frame of the main timeline, changing it “if (thepages._currentframe == 10 && value == ”), and onrelease code (SWFAddress.setValue(’home’))
    directly on the buttons within the navigation movie clip. Does not work. What can the problem possibly be?

    Posted by svetlana | 22 November 2008, 11:18 PM
  3. Hi,

    Sorry you are having problems. Try a couple of things first, and then if you still have problems, zip your files to me at tracyboyer@gmail.com

    It may not work calling to different frames inside a movieclip – have you tried putting these on the base timeline and not having a movieclip to hold all of your content?

    Also, have you tried using trace calls to figure out where the problem originates? This is a great way to figure out what works and what doesn’t.

    Do the buttons work? You may have a _root or _parent problem if you are putting code directly on the buttons. Try moving all of your code to the main timeline.

    Hope this helps! Email me directly if it’s still not working.

    Posted by Tracy Boyer | 23 November 2008, 2:21 AM
  4. Tracy, everything works perfectly now. Shame on me – it was just a simple and stupid mistake that I made in typing.

    Thanks a lot again and sorry to have bothered you.

    Posted by svetlana | 24 November 2008, 4:41 PM
  5. Hi Tracy, I can’t get this to work! I have done everything your tutorial says and I just can’t get it to work. I’m not sure what I am doing wrong…if you have any time to help me I would be very grateful. I have spent weeks trying to get swfaddress to work for my company’s website and I’m about to go insane.

    Posted by Juli | 13 February 2009, 3:51 PM
  6. @all: Juli is using CS4 and is still working on a solution. She will *hopefully* let us know the answer when she gets it to work.

    If you are having problems using deep linking in your project, I suggest opening a new file and making it work on a blank canvas before incorporating it into another file with other code.

    Posted by Tracy Boyer | 22 February 2009, 4:22 PM
  7. Thanks for this awesome tutorial Tracy! Your process has clarified a lot of the concepts on this feature. I am still working on implementing SWFAddress on a small project and your tips are unlocking endless possibilities. Due to the size of the files I am working with I am still having issues on some browsers (i.e.-IE…) but I created a dummy site using just your recommendations and it works like a charm! Thank you!

    Posted by Camilo | 26 February 2009, 2:34 AM
  8. I’ve got the setValue working so that each page brings up it’s individual name but when I use the back button and it scrolls through the values it does not trigger any animation to go back to that page in the swf. Any idea?

    Posted by april | 13 April 2009, 1:45 PM
  9. Hey Tracy!

    nice work…

    Do you know if there is a way to use swfaddress when the buttons are nested in a Movie clip?

    I’m doing Fullbrowser Stuff, therefore my buttons have to be in a MC and I don’t get it fixed (it only works when everything is on the root :-( )

    Thank you…

    Posted by Christoph | 15 April 2009, 11:44 AM
  10. Thanks for easy and useful tutorial. Its very useful to me. It works fine.

    Posted by webcam | 23 October 2009, 3:22 AM
  11. hello my friend nice tutorial
    i was wonder if you were able to help me
    i have already created link system for the ajax that my page is using
    however i want to manage to pass the link to the flash menu state
    i think
    SWFAddress.onChange = function() {
    var value = SWFAddress.getValue();
    var path = SWFAddress.getPath();
    var id = SWFAddress.getParameter(‘id’);
    if (_currentframe == 2 && value == ”) {
    intro.gotoAndPlay(2); //intro is the instance name of our intro movieclip
    } else {
    gotoAndStop(‘$’ + value);
    }
    this is the code required to do so
    can you provide any help?
    is there also a good tutorial for how to use varchars inside flash cause i think i would be able to manage it with varchars?

    Posted by john | 26 January 2010, 12:41 AM
  12. Guys, i know that my post isn’t really relevant, but stil.
    I’ve recently finished my new open source project called SWFSize and i just wanted to share with you guys.
    SWFSize is a small (really small) library for Adobe Flash technology that works in collaboration with SWFObject and provides control over the swf’s container metrics. By controlling the swf’s container metrics SWFSize gives you the ability to solve the MAC OS mouse wheel issue once and for all; don’t use workarounds – use the browser’s native scrollbars instead.
    You can view the samples/get source/docs at the project’s home page.

    Project home: http://chargedweb.com/swfsize/

    ps: but stil your free to delete my post if u’r not interested with it. Cheers.

    Posted by Jloa | 29 January 2010, 12:16 PM
  13. Good read here, thanks Tracy.
    I wrote a tutorial myself at http://bit.ly/aI5QnW if you or your readers are interested.

    Cheers,
    Alex

    Posted by Alex Petrisor | 31 January 2010, 12:26 PM

Post a comment

Go Daddy $6.99.com sale 125x125

Language

Subscribe to RSS Feed

English


Español

Archives