Hi. In answer to your two questions, first things first.
Loading movies
You can load a movie A inside another movie B without loosing the movie A. The best way to do that is by loading your movie B inside a movieclip in movie A. This way:
Let's suppose you have a button (instance name "btn") that will make the loading and the target movie clip is named (instance named) "t" -for "target"-, write this in your main timeline:
-
-
function loadB() {
-
var b:MovieClip = t.loadMovie("movieB.swf");
-
//assuming that the two movies have the same dimensions, position it in 0,0 so they are overlapping perfectly
-
b._x = b._y = 0;
-
//you can pass any parameters you vant, remember that b now is a movielcip within this function.
-
}
-
-
btn.onRelease = loadB;
-
And that's it, pretty simple and very usefull, is the key to making big sites, since you can load each section of the site externally.
Calling ActionScript from JavaScript and vice cersa
To access javascript functions from ActionScript or vice versa, cab be done with the ExternalInterface class that comes bundled and ready to use with since Flash 8. I don't know how to use it in Adobe Flash CS3, but I don't think it's going to be any different.
Here's how:
First of all, in your html embed code you must set the allowScriptAcces property to "always" (i've never tryid if "sameDomain" instead of "always" works too, well, at least it doesn't work in local view of the file).
In the plain html you must set it in two ways fisrt in a param element:
<param name="allowScriptAccess" value="always" />
then, in the embed:
<embed [...] allowScriptAccess="always" />
If you have
SWFObject (which I recommend) you pass the parameter like this:
so.addParam("allowScriptAccess","always");
Now the actionscript part:
The fisrt thing you want to do here is to import the corresponding class in order to use it. The ExternalInterface class it's used directly from the class, you don't need (and it won't work if you do) to instantiate it, because all the methods are static (let's say, the whole class is aware of this methods), let's begin with examples of this fisrt part:
-
//Importing the class:
-
import flash.external.ExternalInterface;
-
//this class has 2 methods and they are used like this:
-
-
//the call method calls a javascript function
-
somebtn.onRelease = function() {
-
var successful = ExternalInterface.call("jsFunction"[,"parameters"]);
-
}
-
-
//the addCallback registers a function to be called from javascript, and this is what you wanted to do:
-
var successful = ExternalInterface.addCallback("methodName",instanceObject, realMethod);
-
-
function realMethod() {
-
//do something
-
}
-
With the call method you call a javascipt function as is, in your javascript you do not need to do anything special., just write a function with the corresponding name.
But when in comes to addCallback there are some other things to have in mind.
The instance parameter for most of the cases just use
this.
The methodName is the name that will be used in JavaScript.
And the realMethod is the call to the real function that will be in the flash movie.
You
always must first add the ExternalInterface callbacks, and then (below) declare the functions.
Now in your js you need first to get the embed element of your flash movie like this:
-
function getMovie(movieName) {
-
if (navigator.appName.indexOf("Microsoft") != -1) {
-
return window[movieName];
-
} else {
-
return document[movieName];
-
}
-
}
-
Or like this if you are using SWFObject:
-
function getMovie(movieName) {
-
return document.getElementById(movieName);
-
}
-
Now we set the function sin javascript that will call the ones in the flash movie. All you got to do now is: get the flash movie and call the function like a method of the movie. Like this:
-
function somefunc() {
-
var flash = getMovie('themoviename');
-
flash.methodName(parametersIfAny);
-
}
-
And that's it, as you can see, it's not too complicated. But if you read till here, let me tell you that I have an example that you can test and download to learn from it here:
http://www.thenephilim.com.ar/tsdn/ei/
To download it go here
http://www.thenephilim.com.ar/tsdn/e...lInterface.rar
Kind regards,
The_Nephilim
Yes.
It works..Thanks Kestrel. But I want your opinion in my scenario. I had two flash file (A and B) . Let say, A supposedly call B and pass certain variables, is it possible I can load B in the same page with A and never unload A.
Thank you in advance.
Hi guys,
How do I access flash function using javascript?Does anyone have any reference or code?
Thanks...