AS3中的动态对象名称

时间:2013-12-27 03:25:33

标签: actionscript-3 flash flex actionscript flex4.5

我的MXML项目中有这个代码,我想为不同的动态用户名获得动态流连接

private var inStream:NetStream;

private function listenStream(user:String):void
            {

                this["inStream"+user] = new NetStream(connection);
                this["inStream"+user].play(user);

            }

private function closeStream(user:String):void
        {

                //clear stream listener
                this["inStream"+user].close();


        }

但是这段代码不起作用,如何在ActionScript3中构建动态对象名?

非常感谢

1 个答案:

答案 0 :(得分:2)

尝试Dictionary

import flash.utils.Dictionary

private var streamDict:Dictionary = new Dictionary();

private function listenStream(user:String):void
{
      var key:String = getKey(user);

      var lastStream:NetStream = streamDict[key] as NetStream;

      if (lastStream)
      {
          //close the last stream or do sth else
      }
      else
      {
         streamDict[key] = new NetStream(connection);
         streamDict[key].play(user);
      }

 }

 private function closeStream(user:String):void
 {
      var key:String = getKey(user);

      //clear stream listener
      var stream:NetStream = streamDict[key] as NetStream ;

      if (stream)
      {
          stream.close();
      }

      //delete the stream
      streamDict[key] = null;
      delete streamDict[key];
 }

 private function getKey(user:String):String
 {
      return  "inStream" + user;
 }