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

热门文章推荐

请稍候...

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

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