AS3 - 如何获取下一个数组索引以及我如何更改我的函数
AS3 - How to get next array Index and how i change my Function
当连接丢失时,我有 connectAgain 功能我添加子按钮,当我单击此按钮调用 connectAgain 功能时,此功能中只有 serverUrl 和 serverPort 只有一个 ip 和端口,这是当前系统及其工作。
但是现在,我有4或5个IP和端口。我想我添加了数组或字典或对象,当连接丢失时我单击按钮并调用数组内的 ip 和端口但是每个连接丢失我想调用下一个 ip 和端口不相同的 ip 和端口。
请帮助我,谢谢,这是我目前的职能。我该怎么做 ?
public static function connectAgain(serverUrl:String = "", serverPort:int = 0):void {
serverUrl = myGameClass.serverUrl;
serverPort = myGameClass.serverPort;
}
您可以使用一些助手 类,如下所示:
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;
}
}
在 init 的某处创建迭代器:
...
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)
...
然后,在重新连接函数中,您可以调用 next()
函数来检索下一个 url 以进行连接:
const nextUri: UriVO = urisToTry.next;
if (nextUri)
connectAgain(nextUri.host, nextUri.port);
else
// you've tried all uris and connection failed.
当连接丢失时,我有 connectAgain 功能我添加子按钮,当我单击此按钮调用 connectAgain 功能时,此功能中只有 serverUrl 和 serverPort 只有一个 ip 和端口,这是当前系统及其工作。
但是现在,我有4或5个IP和端口。我想我添加了数组或字典或对象,当连接丢失时我单击按钮并调用数组内的 ip 和端口但是每个连接丢失我想调用下一个 ip 和端口不相同的 ip 和端口。
请帮助我,谢谢,这是我目前的职能。我该怎么做 ?
public static function connectAgain(serverUrl:String = "", serverPort:int = 0):void {
serverUrl = myGameClass.serverUrl;
serverPort = myGameClass.serverPort;
}
您可以使用一些助手 类,如下所示:
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;
}
}
在 init 的某处创建迭代器:
...
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)
...
然后,在重新连接函数中,您可以调用 next()
函数来检索下一个 url 以进行连接:
const nextUri: UriVO = urisToTry.next;
if (nextUri)
connectAgain(nextUri.host, nextUri.port);
else
// you've tried all uris and connection failed.