我正在构建一个多人游戏,当连接到服务器时,服务器发送回一个可用房间列表(每个房间都有MaxPlayers,MinRank,TableId,TableName,Password),所以每次我收到这5个字符串时,我都会创建一个Mxml UI组件的实例,并用相关的详细信息填充它。
在main.MXML中,我添加了一个AS3脚本变量来保存rcvd数据从服务器传回时创建的GameInstances对象:
private var gameInstances:ArrayCollection = new ArrayCollection();
GameInstance.mxml是一个组件,其中包含UI组件和用于设置某些数据的AS3脚本。在main.mxml中从服务器接收数据时:
var gameInstance:GameInstance = new GameInstance();
gameInstance.setTablePlayers(rcvdMsg[1]);
gameInstance.setTableMinRank(rcvdMsg[2]);
gameInstance.setTableId(rcvdMsg[3]);
gameInstance.setTableName(rcvdMsg[4]);
gameInstance.setTablePassword(rcvdMsg[5]);
gameInstances.addItem(gameInstance);
gameInstances保存该mxml组件的对象。如何在main.mxml上可视化地显示这个组件?我有一个in main.mxml,我想在它里面可视化地显示GameInstance对象。
这就是GameInstance.mxml的样子,我想让s:List为每个游戏保存一个这样的UI对象(当然是为了显示它)
发布于 2011-10-13 23:22:04
好的,如果您使用gameInstances数组集合作为tableList的数据提供程序,那么您需要做一些事情。
首先,您需要使您的gameInstances数组集合可绑定
然后,您需要将数据添加到数组集合中,正如您发布的代码所显示的那样。
接下来,您必须为您的tableList创建自定义项目渲染器。
最后,当你完成修改数据/在gameInstances数组集合中添加/删除对象时,你需要使用gameInstances.refresh();
编辑
创建一个名为myListRenderer.mxml的文件,并将以下代码放入其中
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
override protected function commitProperties():void{
// this is where you assign your values
// this function gets called every-time the list scrolls
super.commitProperties();
players.text = this.data.setTablePlayers
minRank.text = this.data.setTableMinRank;
tableId.text = this.data.setTableId;
tableName.text = this.data.setTableName;
tablePassword.text = this.data.setTablePassword;
}
]]>
</mx:Script>
<mx:Label id="players" />
<mx:Label id="minRank" />
<mx:Label id="tableId" />
<mx:Label id="tableName" />
<mx:Label id="tablePassword" />
</mx:VBox>
发布于 2011-10-07 10:02:42
如果我理解这个问题,当您在ActionScript中创建组件时,您必须在它们在UI中显示之前将它们添加到容器中。如果您有一个组件数组,您只需循环遍历该数组并将每个组件作为子组件添加到容器中即可。主应用程序文件是一个容器,所以您可以这样做:
for each(var myComp:UIComponent in myArrayList){
addChild(myComp);
}
采用这种方法是不寻常的。通常,您只需在创建组件时将其添加到父容器中。
https://stackoverflow.com/questions/7682123
复制相似问题