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

[AS3]as3与js通信源代码(2)

时间:2014-10-28 08:55未知
里边的参数就是embed标签里边的name和object标签里边的id。 另外一种方法是: window.document.myFlash myFlash,也是embed标签里边的name和object标签里边的id。不知道还有其他的方法没。对js不了解。获取到对swf

  里边的参数就是embed标签里边的name和object标签里边的id。

  另外一种方法是:

  1. window.document.myFlash 

myFlash,也是embed标签里边的name和object标签里边的id。不知道还有其他的方法没。对js不了解。获取到对swf的引用,就可以去调用as里边的方法了。直接在后边接 "." ,再接AS那边的方法名,再接里边的参数。也可以不需要参数。看个人需要。

这里还有ifrme的调用。对iframe也很陌生。知道,通过调用,可以在网页调用的swf上覆盖一层页面。这里,可以做flash有时候不方便做,而页端方便做的事情。却不影响美观。具体搞法,看代码。

下边贴出完整的代码:

AS_code:

  1. package 
  2. { 
  3.     import flash.display.Sprite; 
  4.     import flash.display.StageAlign; 
  5.     import flash.display.StageScaleMode; 
  6.     import flash.events.MouseEvent; 
  7.     import flash.external.ExternalInterface; 
  8.     import flash.text.TextField; 
  9.     import flash.text.TextFieldAutoSize; 
  10.     import flash.text.TextFieldType; 
  11.     import flash.text.TextFormat; 
  12.      
  13.     public class AsToJs extends Sprite 
  14.     { 
  15.         private var sendBtn:Button; 
  16.         private var _txt:TextField; 
  17.         private var _txtFormat:TextFormat; 
  18.         private var _inputTxt:TextField; 
  19.         private var _step:int = 0 ; 
  20.          
  21.         public function AsToJs() 
  22.         { 
  23.             stage.scaleMode = StageScaleMode.NO_SCALE; 
  24.             stage.align = StageAlign.TOP_LEFT; 
  25.              
  26.             if(ExternalInterface.available) 
  27.             { 
  28.                 ExternalInterface.addCallback("receiveMes",asReceiveMes); 
  29.                 ExternalInterface.addCallback("backMes",jsBackMes); 
  30.             } 
  31.             Init(); 
  32.         } 
  33.          
  34.         private function callJs(str:String):void 
  35.         { 
  36.             var len:int = str.length; 
  37.             var tempArr:Array; 
  38.             var _url:String; 
  39.             var _x:Number; 
  40.             var _y:Number; 
  41.             var _w:Number; 
  42.             var _h:Number; 
  43.              
  44.             tempArr = str.split("-"); 
  45.              
  46.             switch(int(tempArr[0])) 
  47.             { 
  48.                 case 1: 
  49.                     if(_step ==0 || _step == 3) 
  50.                     { 
  51.                         _url = tempArr[1]; 
  52.                         ExternalInterface.call("loadIFrame",_url); 
  53.                         if(_step !=3) 
  54.                         { 
  55.                             _step = 1; 
  56.                         } 
  57.                         printf("加载 Iframe"); 
  58.                     }     
  59.                     break; 
  60.                 case 2: 
  61.                     if(_step == 1) 
  62.                     { 
  63.                         ExternalInterface.call("showIFrame"); 
  64.                         _step = 2; 
  65.                         printf("显示 Iframe"); 
  66.                     } 
  67.  
  68.                     break; 
  69.                 case 3: 
  70.                     if(_step >= 2) 
  71.                     { 
  72.                         _x = int(tempArr[1]); 
  73.                         _y = int(tempArr[2]); 
  74.                         _w = int(tempArr[3]); 
  75.                         _h = int(tempArr[4]); 
  76.                         ExternalInterface.call("moveIFrame",_x,_y,_w,_h); 
  77.                         _step = 3; 
  78.                         printf("移动 Iframe"); 
  79.                     } 
  80.                     break; 
  81.             } 
  82.         } 
  83.          
  84.         private function asReceiveMes(str:String):void 
  85.         { 
  86.             printf("<font color='#ff0000'>JS发送,AS接受: </font>" + str); 
  87.         } 
  88.          
  89.         private function jsBackMes(str:String):void 
  90.         { 
  91.             printf("<font color='#ff0000'>AS发送,JS返回: </font>" + str); 
  92.         } 
  93.          
  94.         private function Init():void 
  95.         { 
  96.             InitBtn(); 
  97.             InitTxt(); 
  98.             printf("<font color='#ff0000'>Hi,Baby.This is a Demo. </font>"); 
  99.         } 
  100.          
  101.         private function InitBtn():void 
  102.         { 
  103.             sendBtn = new Button("发送信息"); 
  104.             addChild(sendBtn); 
  105.              
  106.             sendBtn.x = stage.stageWidth - sendBtn.width -10; 
  107.             sendBtn.y = stage.stageHeight - 36; 
  108.             sendBtn.buttonMode = true; 
  109.             sendBtn.addEventListener(MouseEvent.CLICK,clickHandler); 
  110.         } 
  111.          
  112.         private function clickHandler(e:MouseEvent):void 
  113.         { 
  114.             if(ExternalInterface.available) 
  115.             { 
  116.                 var numCode:int = int(_inputTxt.text.charAt(0)); 
  117.                 if(numCode > 0) 
  118.                 { 
  119.                     callJs(_inputTxt.text); 
  120.                 } 
  121.                 else 
  122.                 { 
  123.                     ExternalInterface.call("sendMes",_inputTxt.text); 
  124.                 } 
  125.                 _inputTxt.text = ""; 
  126.             } 
  127.         } 
  128.         Boolean 
  129.         private function printf(...args):void 
  130.         { 
  131.             var len:int = args.length; 
  132.             for(var i:int =0 ; i< len ; i++) 
  133.             { 
  134.                 if(_txt.numLines > 20) 
  135.                 { 
  136.                     _txt.text = ""; 
  137.                 } 
  138.                 _txt.htmlText += (args[i] + "\n"); 
  139.             }     
  140.         } 
  141.          
  142.         private function InitTxt():void 
  143.         { 
  144.             _txtFormat = new TextFormat(); 
  145.             _txtFormat.color = 0x00ff00; 
  146.             _txtFormat.size = 13; 
  147.              
  148.             _txt = new TextField(); 
  149.             _txt.autoSize = TextFieldAutoSize.LEFT; 
  150.             _txt.defaultTextFormat = _txtFormat; 
  151.             _txt.multiline = true; 
  152.             _txt.wordWrap = true; 
  153.             _txt.border = true; 
  154.             _txt.borderColor = 0x00ffff; 
  155.             _txt.width = stage.stageWidth - 20; 
  156.             _txt.height = stage.stageHeight - 50; 
  157.             addChild(_txt); 
  158.              
  159.             _txt.x = 10; 
  160.             _txt.y = 10; 
  161.              
  162.             _inputTxt = new TextField(); 
  163.             _inputTxt.autoSize = TextFieldAutoSize.LEFT; 
  164.             _txtFormat.color = 0xff0000; 
  165.             _inputTxt.defaultTextFormat = _txtFormat; 
  166.             _inputTxt.type = TextFieldType.INPUT; 
  167.             _inputTxt.wordWrap = true; 
  168.             _inputTxt.border = true; 
  169.             _inputTxt.borderColor = 0x00ffff; 
  170.             _inputTxt.width = stage.stageWidth - 100; 
  171.             addChild(_inputTxt); 
  172.              
  173.             _inputTxt.x = 10; 
  174.             _inputTxt.y = sendBtn.y; 
  175.         } 
  176.     } 
  177. } 
  178.  
  179.  
  180. import flash.display.Sprite; 
  181. import flash.events.MouseEvent; 
  182. import flash.text.TextField; 
  183. import flash.text.TextFieldAutoSize; 
  184. import flash.text.TextFormat; 
  185.  
  186. class Button extends Sprite 
  187. { 
  188.     private var _txt:TextField; 
  189.     private var _txtFormat:TextFormat; 
  190.     private var _deep:Sprite; 
  191.     private var _spaceW:int = 6; 
  192.     private var _spaceH:int = 3; 
  193.      
  194.     public function Button(label:String):void 
  195.     { 
  196.         _txtFormat = new TextFormat(); 
  197.         _txtFormat.color = 0x00ff00; 
  198.         _txtFormat.size = 13; 
  199.          
  200.         _txt = new TextField(); 
  201.         _txt.autoSize = TextFieldAutoSize.LEFT; 
  202.         _txt.text = label; 
  203.         _txt.setTextFormat(_txtFormat); 
  204.         _txt.mouseEnabled = false; 
  205.         addChild(_txt); 
  206.          
  207.         draw(); 
  208.         this.addEventListener(MouseEvent.ROLL_OVER,overHandler); 
  209.         this.addEventListener(MouseEvent.ROLL_OUT,outHandler); 
  210.     } 
  211.      
  212.     private function draw():void 
  213.     { 
  214.         _deep = new Sprite(); 
  215.         _deep.graphics.clear(); 
  216.         _deep.graphics.lineStyle(1.5,0x00ffff,0.8); 
  217.         _deep.graphics.beginFill(0xff00ff,1); 
  218.         _deep.graphics.drawRoundRect(0,0,_txt.textWidth + _spaceW *2 ,_txt.textHeight + _spaceH *2,5,5); 
  219.         _deep.graphics.endFill(); 
  220.         addChildAt(_deep,0); 
  221.         _txt.x = _spaceW -1.5; 
  222.         _txt.y = _spaceH - 1.5; 
  223.     } 
  224.      
  225.     private function overHandler(e:MouseEvent):void 
  226.     { 
  227.         _txt.alpha = 0.7; 
  228.     } 
  229.      
  230.     private function outHandler(e:MouseEvent):void 
  231.     { 
  232.         _txt.alpha = 1; 
  233.     } 
  234. } 

热门文章推荐

请稍候...

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

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