Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

jquery - How to call flash actionscript callback method from javascript?

I tried to call a flash callback method from JavaScript. But it seems not working. The flash action script example code is like below [Simplified]:

import flash.events.ActivityEvent; 
import flash.events.StatusEvent; 
import flash.external.ExternalInterface;

var test_var = ExternalInterface.addCallback("js_method_to_call", flash_method);


function flash_method()
{   
  return "test"; 
}

The javascript example code is written below [Simplified]:

 function callFlashMethod(){
   var flashFile = eval("window.document.test");
   flashFile.js_method_to_call;
 }
 function loadTest(){
   swfobject.embedSWF("test.swf", "test", "1", "1", "10.0.0", false);
 }

 $(document).ready(function(){
   loadTest();
   callFlashMethod();
 });

It is always display the error in fire bug console "flashFile.js_method_to_call is not a function".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's something that should work really good:

  1. Use SWFObject.js for embedding the Flash content:

    // Embedding through SWFObject rocks in comparison with Adobe shits:
    var flashvars = {};
    
    var params                  =   {};
    params.menu                 =   "false";
    params.salign               =   "t";
    params.scale                =   "noscale";
    params.wmode                =   "transparent";
    params.allowScriptAccess    =   "always";
    
    var attributes              =   {};
    attributes.id = "${swf}";
    
    swfobject.embedSWF("${swf}.swf", "flashDiv", "${width}", "${height}", "9.0.0", "", flashvars, params, attributes);
    
  2. Use this for the HTML:

    <body>
        <div id="flashDiv"></div>
    </body>
    
  3. To call your Flash method use this pattern:

    // Functions needed for calling Flex ExternalInterface
    function thisMovie(movieName) 
    {
        if (navigator.appName.indexOf("Microsoft") != -1) 
        {
            return window[movieName];
        } 
        else 
        {
            return document[movieName];
        }
    }
    
  4. Call the Flash method:

    function callFlashMethod()
    {
        thisMovie("${swf}").js_method_to_call();
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...