如何在不使用 get 、 set 方法的情况下从 as3 中的函数获取变量?

发布于 2024-12-13 14:01:22 字数 2205 浏览 0 评论 0原文

我在从函数(addText)中检索值时遇到麻烦。i 从另一个函数 onFullScreen() 调用。我不知道我该怎么做,请帮助我?这里我附上我的代码

private function addText()
    {


        nc = new NetConnection();
        nc.addEventListener(NetStatusEvent.NET_STATUS, ncOnStatus);



        function ncOnStatus(infoObject:NetStatusEvent)
        {
            trace("nc: "+infoObject.info.code+" ("+infoObject.info.description+")");

            if (infoObject.info.code == "NetConnection.Connect.Success")
            {
                initSharedObject(chatSharedObjectName);
            }


        }
        function formatMessage(chatData:Object)
        {

            trace("room"+chatData.message);
            number = chatData.txtalign;//i want to retrive the value of number
            number.toString();
            return number;


        }
        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;
            }

        }

        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)


        }
                    }

,我使用另一个函数中的变量,但是我无法检索该值。

    private function onFullScreen(event:FullScreenEvent):void
    {
                       mediaContainer.addMediaElement(alert);
            alert.alert("Error",number);// if i cannot retrive the value  hnumber here
             }

Hai am Getting trouble to retrive the values from function(addText).i Called from another function onFullScreen().I dont know how Can i do this,Kindly Help me?Here i attach my Code

private function addText()
    {


        nc = new NetConnection();
        nc.addEventListener(NetStatusEvent.NET_STATUS, ncOnStatus);



        function ncOnStatus(infoObject:NetStatusEvent)
        {
            trace("nc: "+infoObject.info.code+" ("+infoObject.info.description+")");

            if (infoObject.info.code == "NetConnection.Connect.Success")
            {
                initSharedObject(chatSharedObjectName);
            }


        }
        function formatMessage(chatData:Object)
        {

            trace("room"+chatData.message);
            number = chatData.txtalign;//i want to retrive the value of number
            number.toString();
            return number;


        }
        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;
            }

        }

        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)


        }
                    }

i using the variable in another function ,but I cannot retrive the Value.

    private function onFullScreen(event:FullScreenEvent):void
    {
                       mediaContainer.addMediaElement(alert);
            alert.alert("Error",number);// if i cannot retrive the value  hnumber here
             }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为人所爱 2024-12-20 14:01:22

addText() 方法是异步的,这意味着您不能简单地调用它,您需要等待事件侦听器返回值。

我不确定为什么您会觉得需要包含所有这些功能,它不是很清晰,我怀疑这是必要的。您还缺少相当多的分号...

无论如何,我看不到调用 formatMessage() 方法的位置,这似乎是定义“number”变量的唯一位置。

The addText() method is asynchronous, meaning that you can't simply call it , you need to wait for the event listener to return a value.

I'm not sure why you would feel the need to enclose all these functions, it's not very legible and I doubt it's necessary. You're also missing quite a few semi colons...

In any case , I couldn't see where the formatMessage() method was called, it seems that's the only place where the "number" variable gets defined.

强者自强 2024-12-20 14:01:22

您可以在函数范围之外创建变量。

private var num:int;

然后在 addText 函数中,为变量赋值:

num = infoObject.info.code;

然后在 onFullScreen 函数中,访问 num 变量:

 alert.alert("Error", num);

You could create a variable outside the scope of the functions.

private var num:int;

Then in your addText function, assign a value to the variable:

num = infoObject.info.code;

Then in your onFullScreen function, access the num variable:

 alert.alert("Error", num);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文