·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]Flash Player 与 HTML 容器之间发送数据的过程

时间:2015-07-08 21:34酷播
以下示例演示了在 Flash Player 与 HTML 容器之间发送数据的过程。

以下示例演示了在 Flash Player 与 HTML 容器之间发送数据的过程。

  1. package { 
  2.     import flash.display.Sprite; 
  3.     import flash.events.*; 
  4.     import flash.external.ExternalInterface; 
  5.     import flash.text.TextField; 
  6.     import flash.utils.Timer; 
  7.     import flash.text.TextFieldType; 
  8.     import flash.text.TextFieldAutoSize; 
  9.  
  10.     public class ExternalInterfaceExample extends Sprite { 
  11.         private var input:TextField; 
  12.         private var output:TextField; 
  13.         private var sendBtn:Sprite; 
  14.  
  15.         public function ExternalInterfaceExample() { 
  16.             input = new TextField(); 
  17.             input.type = TextFieldType.INPUT; 
  18.             input.background = true
  19.             input.border = true
  20.             input.width = 350
  21.             input.height = 18
  22.             addChild(input); 
  23.  
  24.             sendBtn = new Sprite(); 
  25.             sendBtn.mouseEnabled = true
  26.             sendBtn.x = input.width + 10; 
  27.             sendBtn.graphics.beginFill(0xCCCCCC); 
  28.             sendBtn.graphics.drawRoundRect(0, 0, 80, 18, 10, 10); 
  29.             sendBtn.graphics.endFill(); 
  30.             sendBtn.addEventListener(MouseEvent.CLICK, clickHandler); 
  31.             addChild(sendBtn); 
  32.  
  33.             output = new TextField(); 
  34.             output.y = 25
  35.             output.width = 450
  36.             output.height = 325
  37.             output.multiline = true
  38.             output.wordWrap = true
  39.             output.border = true
  40.             output.text = "Initializing...\n"
  41.             addChild(output); 
  42.  
  43.             if (ExternalInterface.available) { 
  44.                 try { 
  45.                     output.appendText("Adding callback...\n"); 
  46.                     ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript); 
  47.                     if (checkJavaScriptReady()) { 
  48.                         output.appendText("JavaScript is ready.\n"); 
  49.                     } else { 
  50.                         output.appendText("JavaScript is not ready, creating timer.\n"); 
  51.                         var readyTimer:Timer = new Timer(100, 0); 
  52.                         readyTimer.addEventListener(TimerEvent.TIMER, timerHandler); 
  53.                         readyTimer.start(); 
  54.                     } 
  55.                 } catch (error:SecurityError) { 
  56.                     output.appendText("A SecurityError occurred: " + error.message + "\n"); 
  57.                 } catch (error:Error) { 
  58.                     output.appendText("An Error occurred: " + error.message + "\n"); 
  59.                 } 
  60.             } else { 
  61.                 output.appendText("External interface is not available for this container."); 
  62.             } 
  63.         } 
  64.         private function receivedFromJavaScript(value:String):void { 
  65.             output.appendText("JavaScript says: " + value + "\n"); 
  66.         } 
  67.         private function checkJavaScriptReady():Boolean { 
  68.             var isReady:Boolean = ExternalInterface.call("isReady"); 
  69.             return isReady; 
  70.         } 
  71.         private function timerHandler(event:TimerEvent):void { 
  72.             output.appendText("Checking JavaScript status...\n"); 
  73.             var isReady:Boolean = checkJavaScriptReady(); 
  74.             if (isReady) { 
  75.                 output.appendText("JavaScript is ready.\n"); 
  76.                 Timer(event.target).stop(); 
  77.             } 
  78.         } 
  79.         private function clickHandler(event:MouseEvent):void { 
  80.             if (ExternalInterface.available) { 
  81.                 ExternalInterface.call("sendToJavaScript", input.text); 
  82.             } 
  83.         } 
  84.     } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器