【问题标题】:AS3 - How to get next array Index and how i change my FunctionAS3 - 如何获取下一个数组索引以及如何更改我的函数
【发布时间】:2018-01-25 15:14:48
【问题描述】:

连接丢失时我有 connectAgain 功能我添加子按钮,当我单击此按钮时调用 connectAgain 功能在此功能中只有一个 ip 和端口,这是当前系统及其工作。

但现在,我有 4 或 5 个 ip 和端口。我想我添加了数组或字典或对象,当连接丢失时,我单击按钮并调用数组内的 ip 和端口,但每个连接丢失我想调用下一个 ip 和端口,而不是相同的 ip 和端口。

请帮助我,谢谢这是我目前的功能。我该怎么做 ?

public static function connectAgain(serverUrl:String = "", serverPort:int = 0):void {


        serverUrl = myGameClass.serverUrl;
        serverPort = myGameClass.serverPort;


    }

【问题讨论】:

    标签: arrays actionscript-3 flash server


    【解决方案1】:

    你可以使用一些帮助类如下:

    class UriIterator {
        private var _availableAddresses: Vector.<UriVO> = new Vector.<UriVO>();
    
        public function withAddress(host: String, port: int): UriIterator {
            const a: UriVO = new UriVO(host, port);
            _availableAddresses.push(a);
            return this;
        }
    
        public function get next(): UriVO
        {
            return _availableAddresses.length ? _availableAddresses.pop() : null;
        }
    }
    
    class UriVO  {
        private var _host: String;
        private var _port: int;
        public function Address(host: String, port: int) {
            _host = host;
            _port = port;
        }
    
        public function get host():String {
            return _host;
        }
    
        public function get port():int {
            return _port;
        }
    }
    

    在初始化的某个地方创建迭代器:

    ...
    const urisToTry: UriIterator = new UriIterator()
            .withAddress("http://urlone.com", 1211)
            .withAddress("http://urltwo.com", 1212)
            .withAddress("http://urlthree.com", 1213)
            .withAddress("http://urlfour.com", 1214)
    ...
    

    然后,在 reconnect 函数中,您可以调用 next() 函数来检索下一个 url 以进行连接:

    const nextUri: UriVO = urisToTry.next;
    if (nextUri)
        connectAgain(nextUri.host, nextUri.port);
    else
        // you've tried all uris and connection failed. 
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2019-09-16
    • 2016-02-16
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 2015-02-19
    相关资源
    最近更新 更多