본문 바로가기

OM

Flash Javascript 연동 (ExternalInterface)



JSexemple.as========================================================================================

package
{
 import flash.display.*;
 import flash.events.*;
 import flash.text.*;
 import flash.ui.Keyboard;
 import flash.external.ExternalInterface;
 
 public class JSexemple extends Sprite
 {
  public function JSexemple()
  {
   jsSender_ti.text = "Hello, JS";
   
   jsSender_btn.useHandCursor = true;
   jsSender_btn.addEventListener(MouseEvent.CLICK, clickHandler);
   stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressHandler);
   
   ExternalInterface.addCallback("flashFunction", flashFunction);
  }
 
  private function flashFunction(messageFromJS:String):void
  {
   chat_ta.text += "JS> " + messageFromJS + "\n";
  }
 
  private function enterText():void
  {
   if(sendToJS(jsSender_ti.text))
   {
    chat_ta.text += "flash> " + jsSender_ti.text + "\n";
    jsSender_ti.text = "";
    stage.focus = jsSender_ti;
   }
   else{
    chat_ta.text += "flash> fault!!!\n";
   }
  }
 
  private function sendToJS(messageToJS:String):Boolean
  {
   var result:Boolean = false;
   
   if(ExternalInterface.available)
   {
    ExternalInterface.call("jsFunction", messageToJS);
    result = true;
   }
   
   return result;
  }
 
  private function clickHandler(e:MouseEvent):void
  {
   enterText();
  }
 
  private function keyPressHandler(e:KeyboardEvent):void
  {
   if(e.keyCode == Keyboard.ENTER)
   {
    enterText();
   }
  }
 }
}

=====================================================================================================

JSexemple.html======================================================================================

<script language="JavaScript">

function jsFunction(message)
{
    document.form1.textarea.value += "flash> " + message + "\n";
}

function enterText()
{
    sendToFlash(document.form1.textfield.value);

    document.form1.textarea.value += "JS> " + document.form1.textfield.value + "\n";
    document.form1.textfield.value = "";
    document.form1.textfield.focus();
}

function sendToFlash(message)
{
    document.JSexemple.flashFunction(message);
}

</script>

<table width="600" border="0" cellspacing="0" cellpadding="0">
    <form name="form1">
    <tr>
        <td width="50%" rowspan="2">
            <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="275" height="400" id="JSexemple">
            <param name="movie" value="JSexemple.swf" />
            <param name="quality" value="high" />
            <param name="allowScriptAccess" value="always" />
            <embed src="JSexemple.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="275" height="400" allowScriptAccess="always" showLiveConnect="true" name="JSexemple"></embed>
            </object>
        </td>
        <td width="50&">
            <textarea name="textarea" style="width:270; height:350"></textarea>
        </td>
    </tr>
    <tr>
        <td>
            <input type="text" name="textfield" value="Hello, Flash" onKeyDown="javascript:if(event.keyCode==13){enterText();return false}">
            &nbsp;
            <input type="button" value="Send to Flash" style="width:100; height:22" onClick="enterText()">
        </td>
    </tr>
    </form>
</table>

'OM' 카테고리의 다른 글

외부에서 load된 swf가 MouseEvent를 가릴때  (4) 2008.05.18
캐릭터 이동  (2) 2008.05.11
Yahoo WebAPI를 이용한 날씨 위젯 2  (0) 2008.05.11
Yahoo WebAPI를 이용한 날씨 위젯 1  (1) 2008.05.10
애니메이션  (0) 2008.05.07
생명게임  (0) 2008.05.05
Card Match 2  (0) 2008.05.04
원 그리기 (MouseListener)  (0) 2008.05.02
디지털 시계  (0) 2008.05.02