我有一个纯ActionScript 3问题,但我准备的简化测试用例是在Flex 4中以获得更好的可见性(源代码如下):
由于Flash Player 11 / AIR 3本身支持JSON,因此我决定将使用XML与服务器通信的多人游戏移动到JSON。
但我有一个令人沮丧的问题,给出了两个像
这样的对象 { 9013: [
"OK305894249541",
"OK151358069597",
"OK515549948434",
] },
{ 8991: [
"OK253525117889",
"OK529081480134",
] },
我不知道如何创建一个ActionScript函数,它会为第一个对象报告3,为第二个对象报告2个?
上例中的“9013”和“8991”是游戏编号。
“OKxxxx”字符串代表玩家。
当一个游戏拥有3个玩家时,游戏“满员”,否则为“空置”。
这是我非常简单的 TestCase.mxml :
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init(event)">
<fx:Declarations>
<s:RadioButtonGroup id="_filter" change="handleRadio(event);" />
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
[Bindable]
private var _games:ArrayCollection = new ArrayCollection();
[Bindable]
private var _all:uint = 0;
[Bindable]
private var _vac:uint = 0;
[Bindable]
private var _full:uint = 0;
private function vacantGame(obj:Object):Boolean {
// XXX how to implement?
// XXX return true if obj has less than 3 players
return true;
}
private function fullGame(item:Object):Boolean {
// XXX how to implement?
// XXX return true if obj has exactly 3 players
return true;
}
private function init(event:FlexEvent):void {
var i:uint;
_games.source = OBJ['games'];
_all = _games.length;
_vac = 0;
for (i = 0; i < _games.length; i++)
if (vacantGame(_games[i]))
_vac++;
_full = 0;
for (i = 0; i < _games.length; i++)
if (fullGame(_games[i]))
_full++;
}
private function handleRadio(event:Event):void {
switch (_filter.selection) {
case _allBtn:
_games.filterFunction = null;
break;
case _vacBtn:
_games.filterFunction = vacantGame;
break;
case _fullBtn:
_games.filterFunction = fullGame;
break;
}
_games.refresh();
}
private const OBJ:Object = {
lobby: [
"OK108900197210",
"OK266559712237",
"DE6577",
"DE7981",
"OK225312168135",
"OK20629248715",
"DE7880",
],
games: [
{ 0: [] },
{ 9012: [
"VK48058967",
"MR14315189992643135976",
"OK10218913103",
] },
{ 9013: [
"OK305894249541",
"OK151358069597",
"OK515549948434",
] },
{ 8991: [
"OK253525117889",
"OK529081480134",
] },
{ 8937: [
"OK304672497140",
"VK90145027",
"OK338360548262",
] },
{ 9005: [
"OK40798070412",
"DE7979",
"OK531589632132",
] },
{ 9010: [
"OK357833936215",
] },
]
};
]]>
</fx:Script>
<s:HGroup verticalAlign="baseline">
<s:Label text="Games:" />
<s:RadioButton id="_allBtn" group="{_filter}" label="All: {_all}" selected="true" />
<s:RadioButton id="_vacBtn" group="{_filter}" label="Vacant: {_vac}" />
<s:RadioButton id="_fullBtn" group="{_filter}" label="Full: {_full}" />
</s:HGroup>
</s:Application>
答案 0 :(得分:1)
正确的答案是重新考虑您的数据结构。 放下周围的阵列,所有游戏都有一个唯一的ID。
但如果您坚持如上所述使用它,您可以执行以下操作:
private function vacantGame(obj:Object):Boolean{
for( var gameNumber : String in obj ){
var players : Array = obj[ gameNumber ]
return players.length < 3
}
return false;
}
更好的方法:
games: {
0: [],
9012: [
"VK48058967",
"MR14315189992643135976",
"OK10218913103",
],
9013: [
"OK305894249541",
"OK151358069597",
"OK515549948434",
],
...
}
然后
for( var gameNumber : String in OBJ.games ){
var players : Array = OBJ.games[ gameNumber ];
if( players.length < 3 ){
//vacant
}else{
//full
}
}