as与C#的相互调用

flash脚本的运用让图形更富多元化,但刚接触as脚本的外部调用,试验成功了,想想很简单,但没成功之前,却是牺牲不少的时间和精力。

现把两者相互调用代码给大家,共同分享!要知识两者实现相互调用,最重要的功劳要归于xml啊

C#调用as脚本:

//C# 代码

        private void CSharpCallAs_Load(object sender, EventArgs e)//窗体加载时
        {
            axShockwaveFlash1.Movie = Application.StartupPath + "\\example.swf";
            axShockwaveFlash1.FlashCall += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(axShockwaveFlash1_FlashCall);
        }

        private void axShockwaveFlash1_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)//向as发送请求
        {
      //没有任何代码也可以;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            callFunction("output","why?");//调用as中的output()函数,why?为参数
        }

        private void callFunction(string funName, string arg)
        {
            //C#传给Flash的值
            axShockwaveFlash1.CallFunction("<invoke name=\"" + funName + "\" returntype=\"xml\"><arguments><string>" + arg + "</string></arguments></invoke>");
        }

//as脚本

package{
 import flash.display.*;
 import flash.text.*;
 import flash.external.ExternalInterface;
 
 public class example extends flash.display.MovieClip{
  var tf:TextField;
  public function example(){
   tf=new TextField();
   ExternalInterface.addCallback("output",output);//这是外部接口,让C#可以调用
   //output("feel");
   trace("study!")
  }
  public function output(str:String):void{
   tf.text=str;
   tf.textColor=0xff0000;
   this.addChild(tf);
  }
 }
}

就这几句就可实现C#调用as,不是简单的很吗?

as调用C#

//as脚本

package{
 import flash.display.*;
 import flash.geom.ColorTransform;
 import flash.geom.Transform;
 import flash.net.SharedObject;
 import flash.net.URLRequest;
 import flash.events.MouseEvent;
 import flash.display.MovieClip;
 import flash.external.ExternalInterface;
 
 public class changecolor extends flash.display.MovieClip{
  
  private var rect:Sprite;
  private var mySo:SharedObject;
  
  public function changecolor(){
   rect=new Sprite();
   mySo=SharedObject.getLocal("test1");
   var colorHex:String=mySo.data.colorValue;

   var flushResult:Object=mySo.flush();
   trace("flushResult:"+flushResult);
   trace(mySo.data.colorValue);
   rect.graphics.beginFill(mySo.data.colorValue);
   rect.graphics.drawRect(100,100,200,150);
   rect.buttonMode=true;
   rect.addEventListener(MouseEvent.CLICK,callCSharp);
   rect.graphics.endFill();
   this.addChild(rect);
  }
  
  public function callCSharp(event:MouseEvent):void
  {
   var strResult:String=ExternalInterface.call("callCSharp");
   trace("test");
  }
  }
}

//C#代码

private void axShockwaveFlash1_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml(e.request);
            // get attributes to see which command flash is trying to call
            XmlAttributeCollection attributes = document.FirstChild.Attributes;

            // get function
            String command = attributes.Item(0).InnerText;

            // get parameters
            XmlNodeList list = document.GetElementsByTagName("arguments");

            switch (command)
            {
                case "callCSharp":
                    callCSharp();
                    break;

            }
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            axShockwaveFlash1.Movie = Application.StartupPath +"http://www.cnblogs.com/sunblog/admin/file://changecolor.swf/";

            axShockwaveFlash1.FlashCall+=new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(axShockwaveFlash1_FlashCall);
        }

        public void callCSharp()
        {
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.ShowDialog();
            Color color = colorDialog.Color;           
        }

 看似as调用C#要稍微麻烦一些,对xml的操作大家也要熟悉啊

原文地址:https://www.cnblogs.com/qingliuyu/p/1684937.html