0

我正在创建一个在线游戏,人们登录然后显示当前玩家列表。当用户进入“房间”时,它会调度一个 SFSEvent,其中包含一个 Room 对象,其中用户列表作为该房间中的 User 对象。作为该事件的回调函数,我获取当前用户列表,它是一个数组,切换 View Stack 子索引,然后将用户列表数组包装在 ArrayList 中,然后将其分配给 MXML Spark List 组件的数据源。这是我的代码:

我的 Actionscript 代码部分(PreGame.as):

private function onRoomJoin(event:SFSEvent):void
{
    const room:Room = this._sfs.getRoomByName(PREGAME_ROOM);

    this.selectedChild = waitingRoom;

    /** I know I should be using event listeners
     *  but this is a temporary fix, otherwise
     *  I keep getting null object errors
     *  do to the li_users list not being
     *  created in time for the dataProvider assignment **/
    setTimeout(function ():void {
        const userList:ArrayList = new ArrayList(room.userList);
        this.li_users.dataProvider = userList;  // This is where the error gets thrown
    },1000);
}

我的 MXML 代码:

<?xml version="1.0" encoding="utf-8"?>
    <mx:ViewStack xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"
        initialize="preGame_initializeHandler(event)"
        >

        <fx:Script source="PreGame.as"/>

        <s:NavigatorContent id="nc_loginScreen">

            /** Login Screen Code **/

        </s:NavigatorContent>

        /** Start of Waiting Room code **/
        <s:NavigatorContent id="waitingRoom">
            <s:Panel id="pn_users"
                     width="400" height="400"
                     title="Users">

                /** This is the List in question **/
                <s:List id="li_users"
                        width="100%" height="100%"/>

            </s:Panel>
        </s:NavigatorContent>
    </mx:ViewStack>

但是,我不断收到此错误:

TypeError: Error #1010: A term is undefined and has no properties

任何想法我做错了什么?arrayList 有数据,所以我知道它不是空的/null。

4

1 回答 1

0

使用数据绑定解决时序问题:

<s:List id="li_users" dataProvider="{users}" width="100%" height="100%"/>

然后在你的脚本中:

[Bindable]
public var users:ArrayList;

private function onRoomJoin(event:SFSEvent):void
{
    public room:Room = this._sfs.getRoomByName(PREGAME_ROOM);
    this.selectedChild = waitingRoom;
    users = new ArrayList(room.userList);
}
于 2012-11-10T22:55:30.900 回答