PDA

View Full Version : Flash, Javascript, Do something on variable change...


Splitz
03-29-2005, 11:03 PM
Hello and thank you in advance for your time in reading my question....

I'm trying the free 30 day trial of Flash MX 2004, love it so far.. Was an old Directtor user back in 1994 so its a little familiar.

I am trying to determine how to have flash do something when the value of a variable changes via a javascript update. I did the help's howto RE making a text field in flash, naming the field's variable attribute, making a form that onchange it calls a javascript function that passes the html form input to the Flash file and updates the flash text field...

What I need is a little different, I want to define an invisible variable in the flash file, update the variable's value via javascript and I need flash to recognize when the value of this variable changes and then perform an action. I was looking at on(change) but not sure if that applies to what I need... Do I need to instead make a text field outside of the viewable area and do on(change) on that element?

Thank you,
Ed

FrozenMedia
03-30-2005, 12:09 AM
Object.watch will do exactly as you require:
http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary613.html

Or if you are an AS2 coder, you can also use a setter function:
(see attached zip).

If you use Object.watch(), you can watch a variable on _root, such as Object.watch(_root.someVar);

And use your javascript to set "someVar" with SetVariable() (notice caps):
http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_15683#jtfc

Rich

mcmarc
04-20-2005, 08:55 AM
I'm having the same problem to solve and I tried both of your solutions.
They work, however only on Windows IE and Firefox, but I have to get it working on a Mac too.
Does anyone know a way to talk with javascript to a flash movie on a Mac in Firefox and Safari?

Thanks,
Marc

mcmarc
04-20-2005, 10:34 AM
here's my example:
http://www.soulboarders.com/javascripttoflash/javascripttoflash.html
This works on my Windows machine WinXPpro (IE6 / Firefox 1.0)
but NOT on my Mac OSX10.3.8 (Safari 1.2.4/ Firefox 1.0)

Does anyone have any clue how I can get it working on a Mac?
Please help :)
Marc

ps. you can download the .fla file here: http://www.soulboarders.com/javascripttoflash/

FrozenMedia
04-20-2005, 10:52 AM
For Mac you have to use JavaScript to insert an empty SWF that has a bit of code to communicate via Flash's LocalConnection, this empty SWF recieves it's parameters through ?var=value at the end of it's embed string and communicates these params back to your main movie. It is hard to explain but the concept is fairly simple. The only working example I can think of is in tha YamzBrowser file upload component www.yamago.net/components

mcmarc
04-20-2005, 11:07 AM
the YamzBrowser example doesn't work on my mac. But it does on my windows machine. :(
Are you sure that it should work on Mac?

FrozenMedia
04-20-2005, 11:34 AM
Sorry I should have mentioned that's due to something else... hmm this example is much better:
http://www.mustardlab.com/developer/flash/jscommunication/

mcmarc
04-20-2005, 11:58 AM
thanks a lot! this example rocks! :D