2

我有一个继承自 NetConnection 的类,具有以下功能:

override public function connect(command:String, ... arguments):void
{
    addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
    super.connect(command, arguments);
}

我想要做的实际上是这样的:

override public function connect(command:String, ... arguments):void
{
    m_iTries = 0;
    m_strCommand = command;
    m_arguments = arguments;
    addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
    super.connect(command, arguments);
}

private function onNetStatus(pEvent:NetStatusEvent):void
{
    if (/* some logic involving the code and the value of m_iTries */)
    {
        super.connect(m_strCommand, m_arguments);
    }
    else
    {
        // do something different
    }
}

这在 AS3 中可能吗?如果是这样,怎么做?我将如何声明变量、设置变量、将其传递给函数等?谢谢!

4

1 回答 1

1

像这样的东西connect

 ...
 // Add m_strCommand to the start of the arguments array:
 m_arguments.unshift(m_strCommand); 
 ...

并在onNetStatus

if (/* some logic... */)
{
    // .apply calls the function with first parameter as the value of "this". 
    // The second parameter is an array that will be "expanded" to be passed as 
    // if it were a normal argument list:
    super.connect.apply(this, m_arguments);
}

这意味着调用例如(虚假参数):

myNetConnection.connect("mycommand", 1, true, "hello");

将导致此调用的金额来自onNetStatus

super.connect("mycommand", 1, true, "hello");

更多关于.apply()http ://adobe.ly/URss7b

于 2012-11-19T23:17:37.520 回答