Flash 将变量从一个swf传递到另一个actionscript文件?
我试图将一个变量从我的主swf传递到另一个正在主swf容器中加载的变量。我遵循这一点,它工作正常,我的问题是,如果我在函数中声明了变量,我就无法访问该变量。有人帮我吗 (对不起,编码太大了) 我的编码 我的a.swf编码Flash 将变量从一个swf传递到另一个actionscript文件?,flash,actionscript-3,Flash,Actionscript 3,我试图将一个变量从我的主swf传递到另一个正在主swf容器中加载的变量。我遵循这一点,它工作正常,我的问题是,如果我在函数中声明了变量,我就无法访问该变量。有人帮我吗 (对不起,编码太大了) 我的编码 我的a.swf编码 import flash.display.LoaderInfo; import flash.display.Sprite; import flash.text.TextField; import flash.text.Font; var nc:NetConnectio
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.Font;
var nc:NetConnection = null;
var textchat_so:SharedObject = null;
var lastChatId:Number = 0;
var chatSharedObjectName:String = "textchat";//i can access this variable
var chatText:String = "";
var mcCtr:int = 0;
var align:String="gh";
var msg:String;// i cannot access this variable
listChat.visible = false;
var tickerIdx:int = 0
{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, ncOnStatus);
trace("connect: "+ "xxx");
trace("connect: "+ "xxx");
//chatSharedObjectName = connect.soNameStr.text;
nc.connect("xxx");
}
function ncOnStatus(infoObject:NetStatusEvent)
{
trace("nc: "+infoObject.info.code+" ("+infoObject.info.description+")");
if (infoObject.info.code == "NetConnection.Connect.Success")
{
initSharedObject(chatSharedObjectName);
}
}
// format the text chat messages
function formatMessage(chatData:Object)
{
trace("room"+chatData.txtalign);
var number:String = chatData.user;
align=chatData.txtalign;
var myFormat:TextFormat = new TextFormat();
myFormat.size =(chatData.txtsize);
var tf:MarqueeTextField = new MarqueeTextField();
myFormat.font=chatData.txtfont;
tf.maxChars=100;
tf.text =chatData.message ;
tf.textColor = chatData.user; // <----------------------------------
tf.defaultTextFormat=myFormat;
//tf.width = stage.stageWidth / 2;
tf.width = stage.stageWidth;
tf.height = stage.stageHeight
if(chatData.txtalign=="Left")
{
tf.autoSize ="left";
}
if(chatData.txtalign=="Right")
{
tf.autoSize = "right";
}
//tf.x = tf.y = 100;
//trace(stage.stageWidth);
if( listChat.numChildren >= 0 )
{
//listChat.removeChildAt( 0 );
}
listChat.visible=true;
listChat.addChild(tf);
var t:Timer = new Timer(chatData.txtspeed);
t.addEventListener(
TimerEvent.TIMER,
function(ev:TimerEvent): void
{
tf.text =tf.text.substr(1) + tf.text.charAt(0);
}
);
t.start();
if(listChat!=null)
for (var i:int = listChat.numChildren-2; i >= 0; i--) {
listChat.removeChildAt (i);
}
msg=chatData.txtalign;
return msg;
}
function syncEventHandler(ev:SyncEvent)
{
var infoObj:Object = ev.changeList;
// if first time only show last 4 messages in the list
if (lastChatId == 0)
{
lastChatId = Number(textchat_so.data["lastChatId"]) - 1;
if (lastChatId < 0)
lastChatId = 0;
}
// show new messasges
var currChatId = Number(textchat_so.data["lastChatId"]);
// if there are new messages to display
if (currChatId > 0)
{
var i:Number;
for(i=(lastChatId+1);i<=currChatId;i++)
{
if (textchat_so.data["chatData"+i] != undefined)
{
var chatMessage:Object = textchat_so.data["chatData"+i];
formatMessage(chatMessage);
chatText += "<p>" + msg + "</p>";
trace(msg);
//listChat.htmlText = chatText;
}
}
if (listChat.length > 0)
listChat.verticalScrollPosition = listChat.maxVerticalScrollPosition;
lastChatId = currChatId;
}
}
function connectSharedObject(soName:String)
{
textchat_so = SharedObject.getRemote(soName, nc.uri);
// add new message to the chat box as they come in
textchat_so.addEventListener(SyncEvent.SYNC, syncEventHandler);
textchat_so.connect(nc);
}
function connectSharedObjectRes(soName:String)
{
connectSharedObject(soName);
trace(soName);
}
function initSharedObject(soName:String)
{
// initialize the shared object server side
nc.call("initSharedObject", new Responder(connectSharedObjectRes), soName);
}
我不能理解你代码中的某些部分
- 消息函数的Param不是一个变量,它的调用类似于函数名李>
- 您的变量msg应该是公共的
- 您的函数消息没有特定的返回值
- 消息(消息);这没有道理李>
public var msg:String = "";
function Message(msg:String):void
{
this.msg = msg;// Here you assign directly the message
trace(this.msg);
}
在加载程序代码中,如果希望访问msg变量,则代码应该可以正常工作
提示:您应该使用concat类访问SWF,并使用public方法访问这些变量。如果您有问题,您可以发布更多的代码以更加准确
我希望这对你有用
更新:
我重复一遍,我不太了解您的应用程序是如何构造的,我会做如下工作:
public var msg:String = "";
function Message(msg:String):void
{
this.msg = msg;// Here you assign directly the message
trace(this.msg);
}
main.as
Message.as
。此类将导出swf call message.swfpublic class Message
{
private var _msg:String = "";
//Constructor
public function Message(msg:String)
{
_msg = msg;
}
//Getter method
public function get msg():String
{
return _msg;
}
}
function onLoad(evt:Event):void
{
var loadedM:MovieClip = LoaderInfo(evt.target).content as MovieClip;
trace(loadedM.msg);
}
你的问题不是很清楚,但无论如何,下面这句话永远不会起作用:
var target:DisplayObject = LoaderInfo(evt.target).content as DisplayObject;
trace(target["msg"]);// here it return null value
因为DisplayObject没有msg属性,所以需要使用Object或MovieClip
var target:DisplayObject = event.currentTarget.content as MovieClip;
trace(target.msg);// may work better
//and whilst you're there, you may as well remove the event listener...
…差点忘了,在a.swf中,请确保您的变量是公共的
public var msg:String = "whatever"
我改变了我的编码,先生,如果仍然无法理解,请告诉我如何访问a.swf中的变量msg。或者解释我的代码中的问题是什么?我想调用b.assi中的变量msg。如果我的swf工作正常,我在检索变量时遇到问题。再次,我将更改我的完整代码,请仔细检查Sir抱歉,但不是很清楚。也许@PatrickS answer可以帮助你!我使用了Shared object sir,我可以在formatmessage函数中检索共享对象值。现在我想从b.as中的format message(a.swf)访问变量。如果我在b.as中调用(变量)“chatSharedObjectName”作为其工作正常。但是我无法调用函数中的变量。您理解我的问题吗,先生?