[AS3]as3与js通信源代码(2)
里边的参数就是embed标签里边的name和object标签里边的id。 另外一种方法是: window.document.myFlash myFlash,也是embed标签里边的name和object标签里边的id。不知道还有其他的方法没。对js不了解。获取到对swf
里边的参数就是embed标签里边的name和object标签里边的id。
另外一种方法是:
- window.document.myFlash
myFlash,也是embed标签里边的name和object标签里边的id。不知道还有其他的方法没。对js不了解。获取到对swf的引用,就可以去调用as里边的方法了。直接在后边接 "." ,再接AS那边的方法名,再接里边的参数。也可以不需要参数。看个人需要。
这里还有ifrme的调用。对iframe也很陌生。知道,通过调用,可以在网页调用的swf上覆盖一层页面。这里,可以做flash有时候不方便做,而页端方便做的事情。却不影响美观。具体搞法,看代码。
下边贴出完整的代码:
AS_code:
- package
- {
- import flash.display.Sprite;
- import flash.display.StageAlign;
- import flash.display.StageScaleMode;
- import flash.events.MouseEvent;
- import flash.external.ExternalInterface;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFieldType;
- import flash.text.TextFormat;
- public class AsToJs extends Sprite
- {
- private var sendBtn:Button;
- private var _txt:TextField;
- private var _txtFormat:TextFormat;
- private var _inputTxt:TextField;
- private var _step:int = 0 ;
- public function AsToJs()
- {
- stage.scaleMode = StageScaleMode.NO_SCALE;
- stage.align = StageAlign.TOP_LEFT;
- if(ExternalInterface.available)
- {
- ExternalInterface.addCallback("receiveMes",asReceiveMes);
- ExternalInterface.addCallback("backMes",jsBackMes);
- }
- Init();
- }
- private function callJs(str:String):void
- {
- var len:int = str.length;
- var tempArr:Array;
- var _url:String;
- var _x:Number;
- var _y:Number;
- var _w:Number;
- var _h:Number;
- tempArr = str.split("-");
- switch(int(tempArr[0]))
- {
- case 1:
- if(_step ==0 || _step == 3)
- {
- _url = tempArr[1];
- ExternalInterface.call("loadIFrame",_url);
- if(_step !=3)
- {
- _step = 1;
- }
- printf("加载 Iframe");
- }
- break;
- case 2:
- if(_step == 1)
- {
- ExternalInterface.call("showIFrame");
- _step = 2;
- printf("显示 Iframe");
- }
- break;
- case 3:
- if(_step >= 2)
- {
- _x = int(tempArr[1]);
- _y = int(tempArr[2]);
- _w = int(tempArr[3]);
- _h = int(tempArr[4]);
- ExternalInterface.call("moveIFrame",_x,_y,_w,_h);
- _step = 3;
- printf("移动 Iframe");
- }
- break;
- }
- }
- private function asReceiveMes(str:String):void
- {
- printf("<font color='#ff0000'>JS发送,AS接受: </font>" + str);
- }
- private function jsBackMes(str:String):void
- {
- printf("<font color='#ff0000'>AS发送,JS返回: </font>" + str);
- }
- private function Init():void
- {
- InitBtn();
- InitTxt();
- printf("<font color='#ff0000'>Hi,Baby.This is a Demo. </font>");
- }
- private function InitBtn():void
- {
- sendBtn = new Button("发送信息");
- addChild(sendBtn);
- sendBtn.x = stage.stageWidth - sendBtn.width -10;
- sendBtn.y = stage.stageHeight - 36;
- sendBtn.buttonMode = true;
- sendBtn.addEventListener(MouseEvent.CLICK,clickHandler);
- }
- private function clickHandler(e:MouseEvent):void
- {
- if(ExternalInterface.available)
- {
- var numCode:int = int(_inputTxt.text.charAt(0));
- if(numCode > 0)
- {
- callJs(_inputTxt.text);
- }
- else
- {
- ExternalInterface.call("sendMes",_inputTxt.text);
- }
- _inputTxt.text = "";
- }
- }
- Boolean
- private function printf(...args):void
- {
- var len:int = args.length;
- for(var i:int =0 ; i< len ; i++)
- {
- if(_txt.numLines > 20)
- {
- _txt.text = "";
- }
- _txt.htmlText += (args[i] + "\n");
- }
- }
- private function InitTxt():void
- {
- _txtFormat = new TextFormat();
- _txtFormat.color = 0x00ff00;
- _txtFormat.size = 13;
- _txt = new TextField();
- _txt.autoSize = TextFieldAutoSize.LEFT;
- _txt.defaultTextFormat = _txtFormat;
- _txt.multiline = true;
- _txt.wordWrap = true;
- _txt.border = true;
- _txt.borderColor = 0x00ffff;
- _txt.width = stage.stageWidth - 20;
- _txt.height = stage.stageHeight - 50;
- addChild(_txt);
- _txt.x = 10;
- _txt.y = 10;
- _inputTxt = new TextField();
- _inputTxt.autoSize = TextFieldAutoSize.LEFT;
- _txtFormat.color = 0xff0000;
- _inputTxt.defaultTextFormat = _txtFormat;
- _inputTxt.type = TextFieldType.INPUT;
- _inputTxt.wordWrap = true;
- _inputTxt.border = true;
- _inputTxt.borderColor = 0x00ffff;
- _inputTxt.width = stage.stageWidth - 100;
- addChild(_inputTxt);
- _inputTxt.x = 10;
- _inputTxt.y = sendBtn.y;
- }
- }
- }
- import flash.display.Sprite;
- import flash.events.MouseEvent;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFormat;
- class Button extends Sprite
- {
- private var _txt:TextField;
- private var _txtFormat:TextFormat;
- private var _deep:Sprite;
- private var _spaceW:int = 6;
- private var _spaceH:int = 3;
- public function Button(label:String):void
- {
- _txtFormat = new TextFormat();
- _txtFormat.color = 0x00ff00;
- _txtFormat.size = 13;
- _txt = new TextField();
- _txt.autoSize = TextFieldAutoSize.LEFT;
- _txt.text = label;
- _txt.setTextFormat(_txtFormat);
- _txt.mouseEnabled = false;
- addChild(_txt);
- draw();
- this.addEventListener(MouseEvent.ROLL_OVER,overHandler);
- this.addEventListener(MouseEvent.ROLL_OUT,outHandler);
- }
- private function draw():void
- {
- _deep = new Sprite();
- _deep.graphics.clear();
- _deep.graphics.lineStyle(1.5,0x00ffff,0.8);
- _deep.graphics.beginFill(0xff00ff,1);
- _deep.graphics.drawRoundRect(0,0,_txt.textWidth + _spaceW *2 ,_txt.textHeight + _spaceH *2,5,5);
- _deep.graphics.endFill();
- addChildAt(_deep,0);
- _txt.x = _spaceW -1.5;
- _txt.y = _spaceH - 1.5;
- }
- private function overHandler(e:MouseEvent):void
- {
- _txt.alpha = 0.7;
- }
- private function outHandler(e:MouseEvent):void
- {
- _txt.alpha = 1;
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...