2012-04-07 2 views
2

이 문제에 도움을 주시면 감사하겠습니다.JS의 actionscript 함수 호출

특정 링크를 클릭 할 때 AS 기능을 실행하려고하는데 작동하지 않습니다.

내 객체 생성자에서

, 나는 다음 줄을 가지고 다음과 같이 내가 코드를 실행하고

ExternalInterface.addCallback("methodName",methodName); 

다음과 같이 내 기능 (생성자 외부) 정의 :

function methodName() 
     { 
//functioncode here 
     } 

내 HTML 파일 내에서 자바 스크립트 코드는 다음과 같습니다.

<script type="text/javascript"> 
function callExternalInterface() { 
    thisMovie("swf").methodName(); 
} 
function thisMovie(movieName) { 
    if (navigator.appName.indexOf("Microsoft") != -1) { 
     return window[movieName] 
    } 
    else { 
     return document[movieName] 
    } 
} 
    </script> 

버튼을 호출하기위한 코드 위의 함수 : 플래시 객체는 다음과 같은 방법으로 내장되어

<input type="button" onclick="callExternalInterface()" value="Call ExternalInterface" /> 

: 파이어 버그 콘솔에 따르면

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="655" height="324" align="middle" id="swf" name="swf"> 
    <param name="allowScriptAccess" value="always"> 
    <param name="allowFullScreen" value="false"> 
    <param name="movie" value="flash1.swf"> 
    <param name="menu" value="false"> 
    <param name="quality" value="best"> 
    <param name="bgcolor" value="#f3f3f3"> 
    <embed src="flash1.swf" menu="false" quality="best" bgcolor="#f3f3f3" width="655" height="324" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" id="swf" name="swf"> 
    </object> 

를, 내가 버튼을 클릭하면, 다음과 같은 오류 얻을 :

thisMovie("swf").methodName is not a function

기능을 인식하지 못하는 javascript 오류 인 것 같습니다. 나는이 사이트에서 대부분의 코드를 얻었고 어떤 도움을 주시면 감사하겠습니다. 감사!

+0

는 javascript 함수 callExternalInterface()에 대한 코드입니까? –

+0

죄송합니다. 코드 붙여 넣기가 잘못되었습니다. 거기에 그것을 추가했습니다. –

+0

다른 브라우저를 사용해 보셨습니까? – K2xL

답변

-1

귀하가 불만 사항에 직면 한 이유에 대해 귀하가 불완전하게 제공 한 질문 문제에 대한 설명입니다. 여기

내가 만든 작업 테스트 코드 - 귀하가 제공 한 코드의 일부를 사용 -

를 HTML의 헤더 태그에 자바 스크립트 기능

<script type="text/javascript"> 
     function callExternalInterface() { 
     thisMovie("swf").methodName(); 
     } 

     function thisMovie(movieName) { 
     if(navigator.appName.indexOf("Microsoft") != -1) { 
      return window[movieName] 
     } else { 
      return document[movieName] 
     } 
     } 
</script> 
다음과 같은 방법으로 임베디드

플래시 개체 :

<body bgcolor="#ffffff" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> 
     <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" id="swf" width="550" height="400" align="middle"> 
      <param name="allowScriptAccess" value="Always" /> 
      <param name="movie" value="AS3-JS.swf" /> 
      <param name="quality" value="high" /> 
      <param name="bgcolor" value="#ffffff" /> 
      <embed src="AS3-JS.swf" quality="high" bgcolor="#ffffff" width="550" height="400" swLiveConnect=true id="swf" name="swf" align="middle" allowScriptAccess="Always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> 

그리고 AS 클래스

package { 

import flash.display.MovieClip; 
import flash.external.ExternalInterface; 

public class Main extends MovieClip { 


    public function Main() 
    { 
     // constructor code 
     ExternalInterface.addCallback("methodName",methodName); 
    } 

    private function methodName():void 
    { 
     msg_txt.htmlText = "Called" 
    } 
} 

}

+0

전혀 작동하지 않았다 ... 정의되지 않은 methodName의 속성을 읽을 수 없다. –

1

확인을 여기에 실행중인 샘플 여기 http://www.redcodelabs.com/2012/04/calling-actionscript-method-from-javascript/

파일은/

JS 포스트

package { 
    import flash.display.Sprite; 
    import flash.events.*; 
    import flash.external.ExternalInterface; 
    import flash.text.TextField; 
    import flash.utils.Timer; 
    import flash.text.TextFieldType; 
    import flash.text.TextFieldAutoSize; 

    public class ExternalInterfaceExample extends Sprite { 
     private var input:TextField; 
     private var output:TextField; 
     private var sendBtn:Sprite; 

     public function ExternalInterfaceExample() { 
      input = new TextField(); 
      input.type = TextFieldType.INPUT; 
      input.background = true; 
      input.border = true; 
      input.width = 350; 
      input.height = 18; 
      addChild(input); 

      sendBtn = new Sprite(); 
      sendBtn.mouseEnabled = true; 
      sendBtn.x = input.width + 10; 
      sendBtn.graphics.beginFill(0xCCCCCC); 
      sendBtn.graphics.drawRoundRect(0, 0, 80, 18, 10, 10); 
      sendBtn.graphics.endFill(); 
      sendBtn.addEventListener(MouseEvent.CLICK, clickHandler); 
      addChild(sendBtn); 

      output = new TextField(); 
      output.y = 25; 
      output.width = 450; 
      output.height = 325; 
      output.multiline = true; 
      output.wordWrap = true; 
      output.border = true; 
      output.text = "Initializing...\n"; 
      addChild(output); 

      if (ExternalInterface.available) { 
       try { 
        output.appendText("Adding callback...\n"); 
        ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript); 
        if (checkJavaScriptReady()) { 
         output.appendText("JavaScript is ready.\n"); 
        } else { 
         output.appendText("JavaScript is not ready, creating timer.\n"); 
         var readyTimer:Timer = new Timer(100, 0); 
         readyTimer.addEventListener(TimerEvent.TIMER, timerHandler); 
         readyTimer.start(); 
        } 
       } catch (error:SecurityError) { 
        output.appendText("A SecurityError occurred: " + error.message + "\n"); 
       } catch (error:Error) { 
        output.appendText("An Error occurred: " + error.message + "\n"); 
       } 
      } else { 
       output.appendText("External interface is not available for this container."); 
      } 
     } 
     private function receivedFromJavaScript(value:String):void { 
      output.appendText("JavaScript says: " + value + "\n"); 
     } 
     private function checkJavaScriptReady():Boolean { 
      var isReady:Boolean = ExternalInterface.call("isReady"); 
      return isReady; 
     } 
     private function timerHandler(event:TimerEvent):void { 
      output.appendText("Checking JavaScript status...\n"); 
      var isReady:Boolean = checkJavaScriptReady(); 
      if (isReady) { 
       output.appendText("JavaScript is ready.\n"); 
       Timer(event.target).stop(); 
      } 
     } 
     private function clickHandler(event:MouseEvent):void { 
      if (ExternalInterface.available) { 
       ExternalInterface.call("sendToJavaScript", input.text); 
      } 
     } 
    } 
} 

과 HTML에서 일부 코드

<html lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>ExternalInterfaceExample</title> 
<script language="JavaScript"> 
    var jsReady = false; 
    function isReady() { 
     return jsReady; 
    } 
    function pageInit() { 
     jsReady = true; 
     document.forms["form1"].output.value += "\n" + "JavaScript is ready.\n"; 
    } 
    function thisMovie(movieName) { 
     if (navigator.appName.indexOf("Microsoft") != -1) { 
      return window[movieName]; 
     } else { 
      return document[movieName]; 
     } 
    } 
    function sendToActionScript(value) { 
     thisMovie("ExternalInterfaceExample").sendToActionScript(value); 
    } 
    function sendToJavaScript(value) { 
     document.forms["form1"].output.value += "ActionScript says: " + value + "\n"; 
    } 
</script> 
</head> 
<body onload="pageInit();"> 

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
      id="ExternalInterfaceExample" width="500" height="375" 
      codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"> 
     <param name="movie" value="ExternalInterfaceExample.swf" /> 
     <param name="quality" value="high" /> 
     <param name="bgcolor" value="#869ca7" /> 
     <param name="allowScriptAccess" value="sameDomain" /> 
     <embed src="ExternalInterfaceExample.swf" quality="high" bgcolor="#869ca7" 
      width="500" height="375" name="ExternalInterfaceExample" align="middle" 
      play="true" loop="false" quality="high" allowScriptAccess="sameDomain" 
      type="application/x-shockwave-flash" 
      pluginspage="http://www.macromedia.com/go/getflashplayer"> 
     </embed> 
    </object> 

    <form name="form1" onsubmit="return false;"> 
     <input type="text" name="input" value="" /> 
     <input type="button" value="Send" onclick="sendToActionScript(this.form.input.value);" /><br /> 
     <textarea cols="60" rows="20" name="output" readonly="true">Initializing...</textarea> 
    </form> 

</body> 
</html> 
관련 문제