Wednesday, March 5, 2008

Communicating with the Browser Using Actionscript 3 (part 2)

Actionscript's ExternalInterface class (again)

In the last ActionScript 3.0 tutorial post I was talking about using ActionScript 3.0's ExternalInterface Class to communicate with the browser. In the previous example I set up the Flash SWF file to use the ExternalInterface.call() method to tell the browser to run a JavaScript function called "gimmeAlert". Nothing fancy, but you can probably think of functions in the browser you would love to run from a Flash menu or button bar.

In this example we will tell flash to expose an ActionScript function so it can be called from the browser using JavaScript.

First we will start with the flash file and HTML we built in the first example. We have already done much of the setup for this example. We have a text field set to "dynamic" and we not only published the Flash file and placed it in the HTML file, but we added the 'id' and 'name' values to the AC_FL_RunContent() function in step 5. This step was not needed to run the previous example, but it is for this one. Flash can run JavaScript without a name or id placed on the object in HTML, but JavaScript needs it to talk back to Flash.

All that done we are ready to have JavaScript run an ActionScript function.

  1. Return to the Flash file and go back to your ActionScript on Frame 1 of the "actions" layer.
  2. At the bottom of that code add a new line and create the following function...

function changeTextBox():void
{
text_txt.text = "You clicked the button in the web page";
}


This is the function we will call from the web page.

  1. At the top of the ActionScript code add the following line...

ExternalInterface.addCallback("changeTextBox", changeTextBox);

The first argument in the addCallback function is the name JavaScript will use to call the ActionScript function. The second argument is the function being called within ActionScript itself. These do not have to match. Here is was just easier to use the same name.

That's it for the ActionScript/Flash end of things. On to the JavaScript/HTML.

In the HTML file we are going to add some very basic browser detection (mainly because the makers of the browsers cannot seem to use the same DOM or Document Object Model). This will ensure your JavaScript is referring to the same object when it tries to talk to the Flash file.
  1. Go to the <script> tag we added to the <head> of the HTML file and add some extra space at the top. Add the following code...

var flashPlayer;

function detectFlashPlayer()
{
if(navigator.appName.indexOf("Microsoft") != -1)
{
flashPlayer = window.eiexample;
} else {
flashPlayer = window.document.eiexample;
}
}


This will make it easier to reference the flash file. If the browser is Microsoft Internet Explorer, then the variable flashPlayer is a shortcut to window.eiexample. If it is any other browser then the variable flashPlayer is a shortcut to window.document.eiexample.

Only two more steps (including testing).

  1. In the HTML file, add a button like you would in a form. You do not need a form tag.
  2. In the button code, add an onclick property with the value flashPlayer.changeTextBox()
Here's the code...

<input type="button" value="Click Me" onclick="flashPlayer.changeTextBox()" />

That's it. Upload everything and test your button. The text in the flash file should change to the text in the ActionScript function. The possibilities are endless. You can have a Flash file change page elements in HTML. You can have HTML forms and navigation send information to the Flash file. You could send messages to a Flash video player that send it to defined que points. Wait... I think YouTube/Google Video already do that with Flash. hmmm.

There is only one piece of bad news. I found this note in the online help files at Adobe.

"Note: Adobe AIR currently does not support the ExternalInterface class."

Update on AIR
I love the Flash community. They tend to share knowledge as quickly as they come by it. It had been a while since I had considered the ExternalInterface Class and the fact that it does not work in AIR. There is good reason for this it turns out. It looks like Flash content in AIR files can call JavaScript as part of the AIR file's DOM. If you are interested in more, check out this blog entry... AIR ActionScript / JavaScript Bridge


For now I am going to assume AIR has some other internal system of communicating between a flash file in AIR and an HTML file in AIR. I only recently learned that you can create links in AIR that open an external browser like IE or Firefox.

I'll post with more info as I find out more.

'till then
--Rich

More: News Of The Nose

The last time I mentioned my sinus issues I had only mentioned having an appointment to see the Otolaryngologist on Jan 29th. A lot has changed since then.
I got an earlier appointment and saw the surgeon on the 10th of January.
He put a scope up my nose and declared that I has a sausage stuffed up there.
We scheduled the surgery for February 7th, with an MRI a week before and another CT the day before the actual surgery.

MRI's by the way... are no fun. Everyone complains about the noise. They gave me earplugs, so that was no big deal. The problems I had were with boredom and pain. The boredom comes from staring at the same spot in a blank white tube for an hour and a half. The pain comes from the fact that I cannot sit still. When I lay down, I was told to get comfortable. I did. Well... sorta. I would have been comfortable if I had been able to move after a minute or so. The point where my head was resting was fine at the start, but after 45 minutes it was hurting pretty good. After 90 minutes it hurt like hell. Same with my neck. What I thought was comfortable turned into a major pain by the end of the process. By the middle of the third battery of tests I kept my head still by making sure the pain in the back of my head and in my neck kept coming from exactly the same places. I was using the pain as registration marks to make sure I was in the right spot. There was no way I was going to move and have to do this over again.

Anyways...

I had my surgery on Feb. 7th and it was mostly uneventful. There is the creepy aspect of being given a drug cocktail where you will be awake, talking, walking and not remember anything. I was not afraid of anything bad happening to me, but it's a creepy idea nonetheless.

After surgery I went home feeling great. I could breath through my nose for the first time in quite a while, and probably due to the drugs I was in almost no pain. Also as Gretchen at work would say... surgery was some of the best sleep I've had in a while. No kidding. I got several hours of uninterrupted sleep. No pain, lots or air, no 3 year old waking me up to sit in the bathroom while she pees. I felt so good, I thought we had just waisted money on pain meds that I would never use.

The first couple days went like that. I would feel fine during the day and take one pain pill every once in a while to reduce the tense feeling in my face and then two when I went to bed to cut what little pain I had and make sleeping easier. This lasted until Monday, Feb. 11th.

That Monday I went in for my first post-op visit. I was under the impression this would be a stick the scope up the nose... "things are looking great" "we'll see you in a couple weeks" sort of visit. I even thought it was silly that we were being driven to the appointment by my mother-in-law. I was wrong.

I did not know the purpose of the first post-op visit was to remove the remainder of the packing, which was behind a great deal of scabs and other post surgical garbage. Even with the numbing spray they used, this was a painful experience (much more than post surgery). There was a moment in the process where I had to take an extended break due to getting light-headed from the pain. If I had known how long I was going to be there and how much it would hurt, I also would have brought along my pain medication.

I spent much of the next week recovering. I had barely used any pain meds before that first visit. After, however, I was using the half dose every 4 hours during the day and the full dose every 4 hours at night.

After a week of this, I was starting to feel some better. Then I went outside. Allaina needed me to make an emergency trip to the store. I did not think the cold would effect me so badly, but between the trip to the store and taking out the garbage in the sub-zero weather, I ended up spending the entire next day in bed.

It has now been almost a month since my surgery and I'm finally getting back to normal. Actually, if I measure normal by how I was before surgery, then I am already much better than normal. I can breath through my nose. I get better sleep (except for having my daughter wake me up at 6:30am whether I am teaching or not). I am a night person. The longer it has been since I taught last, the later I stay up. Then when I teach, I'm up at 5am.

I have another appointment with the ENT on Monday the 10th, so keep your fingers crossed.

'till later
--Rich