超级SWF跟踪器-巧用LocalConnection的代码
作者:
超级SWF跟踪器-巧用LocalConnection的代码
FLASH中的trace只能在FLASH IDE中跟踪看到,如果你将SWF发布到网上,是看不到trace的调试信息,这给开发网络FLASH带来的调试上的困难,那有没有好的办法来解决这个问题呢,不管SWF发布到什么地方,打开一个本地SWF跟踪器来接收调试消息那岂不是很好,因为我尝试了各种方法,从SharedObject的本地对象到远程对象到LocalConnection的方法都尝试一遍(使用LocalConnection是从Breeze AddIn中发现的一个“秘密”,感觉SNOW给我了一个提示,发现了其中的“奥秘”),在需要调试的SWF中使用下面的类:
class fj.util.JTracer {
public static var debug:Boolean=true;
private static var _instance:JTracer;
private var _lc:LocalConnection;
private function JTracer(){
this._lc=new LocalConnection();
this._lc.allowDomain=function(domain:String):Boolean{
return true;
}
}
public static function trace(msg:Object):Void{
if(!JTracer.debug){
return;
}
if(JTracer._instance==undefined){
JTracer._instance=new JTracer();
}
JTracer._instance.traceMsg(msg);
}
private function traceMsg(msg:Object):Void{
trace(msg);
this._lc.send("localhost:trace","trace",this._lc.domain(),msg);
}
}
使用方法很简单,就是JTracer.trace("我来试试!");
SWF跟踪器中的代码如下:
import fj.util.JDelegate;
class view.JDebug {
private var _ui:MovieClip;
private var _lc:LocalConnection;
function JDebug(ui){
this._ui=ui;
this.init();
}
private function init(){
this._lc=new LocalConnection();
this._lc.allowDomain=function(domain):Boolean{
return true;
}
this._lc.connect("trace");
this._lc.trace=JDelegate.create(this,onTrace);
}
private function onTrace(domain:String,msg:Object){
this._ui.msg_ta.text+=domain+":"+msg+"\n";
}
}
这个需要放一个组件TextArea名字叫msg_ta,用来显示跟踪的消息!
class fj.util.JTracer {
public static var debug:Boolean=true;
private static var _instance:JTracer;
private var _lc:LocalConnection;
private function JTracer(){
this._lc=new LocalConnection();
this._lc.allowDomain=function(domain:String):Boolean{
return true;
}
}
public static function trace(msg:Object):Void{
if(!JTracer.debug){
return;
}
if(JTracer._instance==undefined){
JTracer._instance=new JTracer();
}
JTracer._instance.traceMsg(msg);
}
private function traceMsg(msg:Object):Void{
trace(msg);
this._lc.send("localhost:trace","trace",this._lc.domain(),msg);
}
}
使用方法很简单,就是JTracer.trace("我来试试!");
SWF跟踪器中的代码如下:
import fj.util.JDelegate;
class view.JDebug {
private var _ui:MovieClip;
private var _lc:LocalConnection;
function JDebug(ui){
this._ui=ui;
this.init();
}
private function init(){
this._lc=new LocalConnection();
this._lc.allowDomain=function(domain):Boolean{
return true;
}
this._lc.connect("trace");
this._lc.trace=JDelegate.create(this,onTrace);
}
private function onTrace(domain:String,msg:Object){
this._ui.msg_ta.text+=domain+":"+msg+"\n";
}
}
这个需要放一个组件TextArea名字叫msg_ta,用来显示跟踪的消息!