Controlling Javascript> CSS via Flash
Ideal situation / setup: A page containing 1 Flash movie and a separate div containing several hyperlinks. These hyperlinks have a unique class name: Copy Code
<ul>
<li><a href="" class="randomname1"></a></li>
<li><a href="" class="randomname2"></a></li>
<li><a href="" class="randomname3"></a></li>
<li><a href="" class="randomname4"></a></li>
</ul>
The Flash movie itself will contain 4 buttons. Clicking on one of these buttons should make Flash interact with JQuery / JS and tell it to highlight the specific class name.
Ideas so far
For javascript it will look like
$(function() {
function setClass(className) {$("."+className).css("background","red");}
});
And at certain keyframes inside Flash
1. button 1
ExternalInterface.call("setClass","randomname1");
1. button 2
ExternalInterface.call("setClass","randomname2");
1. button 3
ExternalInterface.call("setClass","randomname3");
1. button 4
ExternalInterface.call("setClass","randomname4");
The problem is it doesn't work very well and I'm not sure if I'm doing the flash communication with JS properly.
Any ideas or hints to steer me in the right direction again? Thank you in advance
J.
a source to share
The method setClass
will not show up for flash code because the method is wrapped in a closure$(document).ready()
(In case you're wondering, your challenge is $(function()...
just shortened to $(document).ready(function()...
)
You need to attach this method to the window object or any other globally accessible object in order to be visible. Sort of:
$(function() {
window.setClass = function(className) {$("."+className).css("background","red");}
});
which is the simplest approach.
... or to stop polluting the window's namespace:
$(function() {
var ns = window.myNamespace = {};
ns.setClass = function(className) {$("."+className).css("background","red");}
});
.... then in Flash:
ExternalInterface.call("myNamespace.setClass","randomname1");
.... a note, however, that I am not familiar enough with flash to know if the second example will work.
a source to share