External Interface and local connections

External interface is a class in Flash that allows the swf to talk to javascript and vice versa. There a two basic methods:

  1. call - you can call a javascript function from the swf
  2. addCallback - you can call a actionscript function from javascript

So to get it to work first of all you need to import the External Interface class:

import flash.external.ExternalInterface;

Then you need add your calls and callbacks. For a callback:

ExternalInterface.addCallback("actionScriptFunc", this, actionScriptFunc);

In this case the actionScriptFunction in the inverted commas is the name that the javascript will be calling and the second is the function within the actionscript itself. The ‘this’ bit as I understand it refers to the movie itself but I not sure you should worry about it at this stage unless you want to learn a bit more about OOPs. This actionscript function needs to have all the correct parameters as it is call from the javascript e.g.

The javascript:

actionScriptFunc(param1, param2);

The actionscript:

function actionScriptFunc(param1:String, param2:String):Void{
if(param1 && param2){
doStuff();
}
}

So basically you are sending param1 and param2 into the swf so actionScriptFunc can do what it wants with it.

And for a call:

ExternalInterface.call("javascriptFunc", this, param1, param2);

This calls the javascript function ‘javascriptFunc’ with the param1 and param2.

So the javascript function might look like this:

function javascriptFunc(param1, param2){
doStuff(param1, param2);
}

Once you get your head round you should be able to easily send stuff between swfs and javascript. N.B. This only works in Flash Player 8.

The good thing about this is if you want to use a Local connection between two movies it provides a way for you to give the ‘connections’ unique ids so you movies only talk to each other and you can have more than one set of movies running at a time.

To do this you need functions in the movies to set the names of the local connections. You need a sender and a receiver for each movie so you need to set names for these connections. This means the connection name needs to be a variable e.g.

sendingLC.send (senderName);
receivingLC connect( receiverName );

Then there will be two functions in each movie one that sets the senderName and one that’s sets the receiverName. In each movie the connection names are swapped so movie 1 will have the same senderName as movie 2s receiverName and vice versa.

Each of the two functions should set up so it can be called from javascript using the External Interface object sending in randomly generated names/numbers. I try and give them a name that identifies which connection is which so when I am debugging I can see which names are set where. to generate the names I use a fixed name say ‘movie1′ and appeand a random number using something like:

var random_number = Math.round(Math.random()*10000000);

You should really be able to set up the connections without using javascript by using one named connection between the two movies which is only used to send the ids across. I tried doing this originally without much success hence this idea which I have working and seems pretty reliable as well!

Comments

One Response to “External Interface and local connections”

  1. connection

    This is exactly what I was looking for, thanks for the great information.

Leave a Reply

Stuff I Dugg

  • Drivable Star Wars Landspeeder Kicks Ass

  • One Less Windows User

    After nearly five years, I needed to update my personal computer. My old Dell Inspiron has long since been rendered quaint by advances in processor speed and hard drive capacity. Rather than buy a new machine running Windows Vista or take the Mac plunge, I decided to walk the open source walk.

  • 20 Tech Habits to Improve Your Life

    A few new habits can make the difference between staying on task, finding what you need, and getting things done--or having a tech meltdown.

  • Space Cube - The World's Smallest Linux PC

    Meet the Space Cube — the world's smallest fully functional PC. Primarily designed for use in space, it somehow manages to cram a working PC with USB ports, card readers, audio outputs and proprietary interfaces into a tiny cube chassis measuring just two inches square. It runs a basic Linux front-end.

  • How Simple Web Design Helps Your Business

    It’s crucial to have simple web designs to allow the user to quickly find the information they need, especially if you are selling a product. If the page is cluttered with useless text, widgets or unrelated products, the site becomes meaningless.

  • 24 Core, 48GB Linux Render Cluster built inside IKEA Cabinet

    This amazing Linux Cluster consists of 6 Intel Core 2 Quad(2.4GHZ) Processors, 48GB of RAM spread across 6 GIGABYTE GA-G33M-DS2R Micro ATX motherboards and put inside an IKEA Helmer cabinet, WHY YOU ASK? 3D computer rendering is very CPU intensive and the best way so speed up slow render problems, are usually to distribute them on to more computers

  • 25 killer Linux apps

    The very best software for your Linux machine.

  • Lego Star Wars Olympics 2008 [Flickr Photo Set]

    Beijing 2008 Olympic Games

  • Leaked Document Shows Verizon's Anti-iPhone Propaganda

    Verizon's tactics to combat the Jesusphone 3G now include briefing its employees about the iPhone's failings, so they can answer customer questions about why it doesn't carry the device.

  • 10 Good UNIX Usage Habits

    Adopt 10 good habits that improve your UNIX® command line efficiency -- and break away from bad usage patterns in the process. This article takes you step-by-step through several good, but too often neglected, techniques for command-line operations.

Subscribe

Who Am I?

No Sweat Apparel.com