To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Display Modes
Old 06-25-2001, 02:20 AM   #1
catfink
Junior Member
 
Join Date: Jun 2001
Posts: 8
Hi,

How do I call a custom function from javascript? I've placed a function on my main timeline called attachClip(). Also, what if I want to call a function within a clip on the main timeline?

Thanks!

=^..^=
catfink


//Flash main timeline script:
function attachClip()
{
attachMovie("icon_server", "ks", l);
ks._x = 200;
ks._y = 200;
}



// This is where I try to call the ActionScript function
function addIcon()
{
flashObj().attachClip();
}


function flashObj()
{
if(navigator.appName == "Netscape")
{
return document.embeds[0];
}
else
{
return window['flashObject'];
}
}

catfink is offline   Reply With Quote
Old 06-25-2001, 04:12 AM   #2
Stickman
Senior Member
 
Join Date: Jul 2000
Location: Not on the dole any more
Posts: 1,040
Communicating between HTML (or JavaScript) and Flash after the movie has launched requires FSCommand. I suggest you check out moock.org for an introduction. Be aware that FSCommand isn't 100% reliable and doesn't work with all browsers (for example, it's not supported by Navigator 6).
Stickman is offline   Reply With Quote
Old 06-25-2001, 04:55 PM   #3
catfink
Junior Member
 
Join Date: Jun 2001
Posts: 8
Lightbulb

I appreciate your response. I've visited the moock.org site and looked at the fscommand information. It does not provide examples on calling custom actionscript functions from javascript. It only gives examples of calling the built in functions.

I don't think it is a requirement to use FSCommand to call javascript functions from a movie clip. One can also use getURL().

If you have specific examples or URLs that explain how to call movie functions using javascript, I'd greatly appreciate viewing them.

I've purchased two books, ActionScript:the definitive guide, by Colin Moock, and ActionScripting in Flash by Phillip Kerman. Unfortunately, both of these books offer very little in this area. I'm sure it can't be that hard, but I need something to start me in the right direction.

Many thanks

catfink is offline   Reply With Quote
Old 06-25-2001, 05:31 PM   #4
Stickman
Senior Member
 
Join Date: Jul 2000
Location: Not on the dole any more
Posts: 1,040
You can call JavaScript from Flash without FSCommand (with GetURL, as you mentioned), but as far as I'm aware you can't call Flash functions from Javascript without it. I have some examples of how to do this -- I'll dig them out for you tomorrow.
Stickman is offline   Reply With Quote
Old 06-25-2001, 06:12 PM   #5
beelineuk
Senior Member
 
Join Date: Jun 2001
Posts: 136
You can call a JavaScript function from within flash directly using the getURL command as follows:

getURL("JavaScript:myFunction(myParam);");

As far as I know there is no way to call a Flash 5 function from JavaScript. However you can use TCallLabel to call the frame of a flash movie clip from JavaScript, and use code on this frame to pass the call on to your target function.

This can be done as follows where "myMovie" is the name of your embedded flash movie.

<SCRIPT LANGUAGE="JavaScript>;
<!--
function Call2Flash() {
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
var obj = InternetExplorer ? window.myMovie : window.document.myMovie;

obj.TCallLabel ("/", "myLabel");
}
//-->
</SCRIPT>

Hope that helps.

Mike Carlisle
beelineuk is offline   Reply With Quote
Old 06-25-2001, 08:51 PM   #6
catfink
Junior Member
 
Join Date: Jun 2001
Posts: 8
Thanks Mike. Your technique is helpful, but I will definitely have to pass some information to the function as well, so I hope there is a way to do that.

I wanted to create a cool little project using Flash5 to handle my XML and UI stuff and java and javascript to handle the rest. I've bought books on flash and have been learning as much as I could for the past month, so I'll be crushed if I can't do what I had intended. I'm looking forward to your examples, Stickman, and greatly appreciate your efforts. Meanwhile, I'll continue to look for some answers and post the results once I find a concrete answer.

Regards,
catfink is offline   Reply With Quote
Old 06-25-2001, 09:20 PM   #7
catfink
Junior Member
 
Join Date: Jun 2001
Posts: 8
Well, unless someone proves me wrong...I believe Mike was right. According to Macromedia technote 4061, these are the only methods that can be called using javascript.

Play()
StopPlay()
IsPlaying()
GotoFrame (int frameNum)
TotalFrames()
Rewind()
SetZoomRect (int left, int top, int right, int bottom)
Zoom(int percent)
Pan (int x, int y, int mode)
PercentLoaded()


What a bummer.
catfink is offline   Reply With Quote
Old 06-26-2001, 05:49 AM   #8
Stickman
Senior Member
 
Join Date: Jul 2000
Location: Not on the dole any more
Posts: 1,040
I think I'm getting old...

I was 100% sure I had a working example of calling a Flash function directly from JavaScript, but can't find a trace of it (if indeed it ever existed!). However, I have a cunning alternative plan...

One of the mos useful (and, oddly, undocumented) facilities of FSCommand is....wait for it...SetVariable. That's right, you can send variables from JavaScript into Flash. It works like this:

Code:
window.document.flashFileID.SetVariable(variableName,value);
So for example, in my HTML code I've set the movie's ID as testFunction. I want to set the variable myVar to 'yes'. The code would look like this:


Code:
window.document.testFunction.SetVariable('myVar','yes');
How does this help? Well it's not exactly an elegant solution, but you could have a constant loop (using onClipEvent(enterFrame)) which checks the value of a variable, let's say myFunction. When you want to execute a function in Flash, you send the function name to the myFunction variable, and then Flash decides what to do based on a series of if statements for each of the possible function names. You could send the arguments in a separate variable. So for example, here's some JavaScript:

Code:
<SCRIPT LANGUAGE="JavaScript">
<!--
function setAlpha(target, alpha)
{
window.document.testfunc.SetVariable('myFunction','setAlpha');
window.document.testfunc.SetVariable('args',target + ',' + alpha);
}
//-->
</SCRIPT>
This is a simple function to set the Alpha of a movie clip -- it takes two arguments, target and alpha, corresponding to the Instance Name of the target movie clip, and the alpha value we want to set it to.

Now for the actionscript that detects it:

Code:
onClipEvent (enterFrame) {
    myArgs = _root.args.split(',');;
    if (_root.myFunction == 'setAlpha') {
        _root.setAlpha(myArgs[0],myArgs[1]);
    }
}
This splits the variable args by comma and sends the results to the relevant function as appropriate. Finally, the function:

Code:
function setAlpha (mcName,val) {
    _root[mcName]._alpha = val;
}
Okay, so it's not especially elegant but it works. If you need some demo code I can send it.

I hope this helps!
Stickman is offline   Reply With Quote
Old 06-26-2001, 06:20 AM   #9
Stickman
Senior Member
 
Join Date: Jul 2000
Location: Not on the dole any more
Posts: 1,040
It occurs to me that you could simplify this at the JavaScript end, like this:

Code:
<SCRIPT LANGUAGE="JavaScript">
<!--

function flashFunc(func, args)
{
window.document.testfunc.SetVariable('myFunction',func);
window.document.testfunc.SetVariable('args',args);
}

//-->
</SCRIPT>
So it sends whatever function name and arguments you want without having to have a separate JavaScript function for each one. So you can invoke any Flash function with the following syntax:

Code:
flashFunc(FunctionName,arguments)
so for example we could do the same as the setAlpha example above, like this

Code:
flashFunc('setAlpha','box,100')
Assuming of course that the code exists at the Flash end to deal with whatever functions you are calling.
Stickman is offline   Reply With Quote
Old 06-26-2001, 12:36 PM   #10
catfink
Junior Member
 
Join Date: Jun 2001
Posts: 8
Thanks Stickman,

Good idea.

After discovering that I couldn't call flash functions directly, I thought the only way to pass information to my movie was through SetVariable. However, I hadn't thought far enough ahead to realize I could pass in function names and vars. I think this will work for me. Thanks. You've made a future flash geek's life a little brighter today.
catfink is offline   Reply With Quote
Old 07-11-2001, 12:07 PM   #11
LuxFX
proud new daddy!
 
LuxFX's Avatar
 
Join Date: May 2000
Location: Dunsinane
Posts: 1,148
Hi guys, came across this thread as I was searching for something else, and it didn't seem too old so I thought I'd throw in my 2cents. I don't know if this will still be useful at all catfink, but log it away and hopefully it will become useful again.

You can call functions, flash4 style, from javascript. You get to use the TCallFrame( target, frameNumber ) function (alternatively, TCallLabel). It works exactly like the 'call' function of actionscript. Put your code in that frame, and use this code from javascript:

window.document.testfunc.TCallLabel('/','editNames'); //or whatever, of course

Now, I still personally like Stickman's technique, and I've used it before myself. It's a more Flash5-ish, but unfortunately the javascript commands for Flash are antiquated.

Hope this was somewhat useful!
LuxFX is offline   Reply With Quote
Old 07-11-2001, 12:18 PM   #12
rapidcarbon
Senior Member
 
Join Date: Jun 2001
Location: Virginia, USA
Posts: 437
there are 2 tutorials (and are very good one) posted over Tutorial section, on page 5. Teach you how to talk to flash from Javascript and talk to HTML from Flash. Those tutorial teach you how to code your HTML so you can pass var to Flash, etc.

Wonder when people really be using the tutorials here?
rapidcarbon is offline   Reply With Quote
Old 07-11-2001, 12:22 PM   #13
rapidcarbon
Senior Member
 
Join Date: Jun 2001
Location: Virginia, USA
Posts: 437
Oh, one more thing, you can not pass var to Flash unless you name you flash movie in HTML page, like ID='FlashMovie' and NAME='FlashMovie'

Good luck
rapidcarbon is offline   Reply With Quote
Old 07-11-2001, 02:08 PM   #14
catfink
Junior Member
 
Join Date: Jun 2001
Posts: 8
RapidCarbon,

Can you be more specific about which tutorials you are referring to and where they are located?

Just for clarity, I have no problem communicating between JavaScript and Flash or vice versa. I specifically want to pass information to custom Flash functions that I've created (not just call a function or pass info to a built-in function). So far, StickMans approach is the only way I've seen to do this.

Thanks.
catfink is offline   Reply With Quote
Old 07-11-2001, 02:32 PM   #15
LuxFX
proud new daddy!
 
LuxFX's Avatar
 
Join Date: May 2000
Location: Dunsinane
Posts: 1,148
I find the information at http://www.macromedia.com/support/fl...withflash.html to be invaluable.

cheers!
LuxFX is offline   Reply With Quote
Old 07-11-2001, 02:56 PM   #16
rapidcarbon
Senior Member
 
Join Date: Jun 2001
Location: Virginia, USA
Posts: 437
Here are the link for those tuts
http://www.flashkit.com/tutorials/In...77/index.shtml
http://www.flashkit.com/tutorials/In...84/index.shtml
They both in Tut section, page 5, at bottom of the page.

for passing a var to a user defined function in Flash, I have not done it, but this is what I would do
Code:
function userDefined(){
   var myVar = _root.var;
   // codes to be execute for that function
}
in your HTML, you will pass a variable to _root.var in Flash. Your flash file in HTML named FlashTest. and here your JVscript
Code:
<SCRIPT LANGUAGE='JavaScript'>
<!--
	function talkToFlash(myValue) {
		window.document.FlashTest.SetVariable("myVar", myValue);
		window.document.FlashTest.onFocus;
	}
-->

</SCRIPT>
Using somekind of eventHandler to triger that function, then using window.document.FlashTest.onFocus; shift focus to flash movie (for somereason, flash movie won't update variable, unless focus is on it)

This is what I learn from those 2 tuts.

Good luck
rapidcarbon is offline   Reply With Quote
Old 07-11-2001, 02:59 PM   #17
rapidcarbon
Senior Member
 
Join Date: Jun 2001
Location: Virginia, USA
Posts: 437
Oppp, one small error. Change this
Code:
window.document.FlashTest.SetVariable("myVar", myValue);
to this
Code:
window.document.FlashTest.SetVariable("var", myValue);
Because we have _root.var not _root.myVar. By the way, variable var wont' work, it is reserved word. I know I know, I was stupid to name it like that. So change it to something else.

Sorry
rapidcarbon is offline   Reply With Quote
Old 04-03-2006, 11:35 AM   #18
derekcfoley
Member
 
Join Date: Nov 2000
Location: South East UK
Posts: 86
Hate to say this guys... but it seems the FlashPlayer version 7 and 8 have major bugs that apparently stop this methodology working as well as it used to - I know I've had problems for the 18 months using this method, in fact I've had to curtail my flash integration projects as a result of this issue.
I know I had problems back then, but it was the time that Player 6 was around, the only thing the setVariable method didnt work on was IE on the mac.

Ironically, it seems also you can't even send variables in anymore on the url either....which is the most basic way of sending data into a flash movie from html.
e.g. (in html object/embed tags)
"moviename.swf?test=ok&test2=ok"

This is really serious, as a lot of advanced flash apps rely on this communication method...

and by the looks of things it has been ignored by Macromedia/Adobe....
A search of their site returns nothing for the "Error calling method on NPObject!"

After some digging around on the net, I thought I'd post what I found to help... see the following links.

http://flashmx2004.com/forums/index.php?showtopic=8871

http://www.webdeveloper.com/forum/sh...d.php?p=517296

and another useful post that might come from a past colleague of mine here...where Chris suggests delaying the communication slightly - e.g. using a method like this....
<BODY OnLoad=setTimeout("alert('test');flashFunc('docroo t','<?=$DOCROOT?>')",2000);>

However - I just tried the above without success... I'm trying to send in a PHP variable containing the root of the website path into the movie, so that the navigation buttons use the right path, wherever it is called from.

The javascript alert triggers, there are no Javascript errors in firefox, but the flashmovie just sits in a loop waiting for the info and nothing happens... my movie has a dynamic text field that shows the value of "_root.docroot" and it remains blank, while the movie checks its value before proceeding...

perhaps someone else expand on this and could help...(see Chris' post below - Hi Chris if you've seen this!!!)

http://blog.gilluminate.com/?b=20041208120812

----------------------------------------
// Chris Williams December 21, 2005 - 8:34 am
I have noticed that in Firefox (v1.1 and 1.5) if I have many flash elements loading on a page and try to call the SetVariable function from body onLoad I get the following error: "Error calling method on NPObject!" I have since worked it out that this is caused when the call to the flash movie is done before the movie is loaded. To rectify this, I have simply added a setTimeout call to the javascript so that the function is called 1-2 seconds after the page has loaded, but I suppose you could add something into the movie to say it has loaded. I just thought someone might like to know this - and seeing as this board came up quite highly on google when I was trying to fix this issue I thought I would post it here!
------------------------------------------

Regards

Derek Foley
derekcfoley is offline   Reply With Quote
Old 04-03-2006, 02:39 PM   #19
derekcfoley
Member
 
Join Date: Nov 2000
Location: South East UK
Posts: 86
Just managed to find a solution to this nightmare (or at least a partial one!) using the new ExternalInterface Class in Flash 8 instead of SendVariable, or FSCommands

http://www.informit.com/guides/conte...eqNum=340&rl=1

Only problem is, the communication in the examples shown here work great from Flash to Javascript, but the example going from Javascript to Flash however doesnt work, as there seems to be a flaw in the sample code when triggered, it results in - "thisMovie is not defined" in the FireFox javascript console.

Not being a Javascript guru, I can't suggest a fix... Its probably quite easy...perhaps someone can, at least then we might have a solution to the SendVariables issue, even if it means having to use the latest player and authoring tool (v8)

Thanks in advance

Derek Foley
derekcfoley is offline   Reply With Quote
Old 04-03-2006, 04:24 PM   #20
derekcfoley
Member
 
Join Date: Nov 2000
Location: South East UK
Posts: 86
Found a better example... this works communication goes 2 ways!!! Hurrah!!!!

http://www.devx.com/webdev/Article/30339/1954?pf=true

I hope this helps everyone out there who is struggling with Javascript to Flash and Flash to Javascript communication!

Derek Foley
derekcfoley is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:22 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.