AS3从数组中间删除对象而不知道其索引

时间:2013-03-06 21:02:07

标签: arrays actionscript-3

我有以下代码。在onClientClose方法中,我想从_clientSockets数组中删除_socketServicer实例。如果我不知道它的索引,我如何引用数组中的_socketServicer实例?

public function connectHandler(event:ServerSocketConnectEvent):void {                            _socketServicer=new SocketService(event.socket, this,log);
            _socketServicer.addEventListener(Event.CLOSE, onClientClose);
            _clientSockets.push(_socketServicer); //maintain a reference to prevent premature garbage collection
        }

        private function onClientClose(event:Event):void {
            //Nullify references to closed sockets
            for each (var servicer:SocketService in _clientSockets) {
                if (servicer.closed)
                    servicer=null;
            }
        }

1 个答案:

答案 0 :(得分:2)

您可以使用.indexOf()

clientSockets[ clientSockets.indexOf(servicer) ];