如何匹配用户名和密码集(AS3中的SharedObject)

时间:2012-11-10 21:20:28

标签: actionscript-3 actionscript

我目前正在使用SharedObjects在flash cookie中保存用户名和密码。 我的问题是如何使usernameField与passwordField匹配,usernameField2与passwordField2匹配。基本上我想区分一个登录集与另一个登录集。

package 
{

    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;

  public class foo extends Sprite 
  {

      public var usernameField:TextField = new TextField();
      public var passwordField:TextField = new TextField();
      public var conPasswordField:TextField = new TextField();
      public var traceOutputField:TextField = new TextField();

      public var saveDataObject:SharedObject = SharedObject.getLocal("test");

      public function foo() 
      {
           traceOutputField.width = 500;
           addChild(traceOutputField);

           usernameField.text = "username";
           passwordField.text = "password";
           conPasswordField.text = "password";

           usernameField.y = 50; addChild(usernameField);
           passwordField.y = 100; addChild(passwordField);
           conPasswordField.y = 150; addChild(conPasswordField);

           registerUserInfo(null);
      }

      public function registerUserInfo(evt:MouseEvent):void
      {
           if(passwordField.text == conPasswordField.text)
           {                      
                  saveDataObject.data.savedUsername = ""+usernameField.text;
                  saveDataObject.flush();
                  traceOutputField.text = "DEBUG " + saveDataObject.size + " / " + saveDataObject.data.savedUsername;

                  saveDataObject.data.savedPassword = ""+passwordField.text;
                  saveDataObject.flush();
                  trace(saveDataObject.size);
           }
           else if(passwordField.text != conPasswordField.text)
           {
                  trace("Passwords do not match");
           }
      }

  }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

public var saveDataObject:SharedObject = SharedObject.getLocal("test");

public function getUsers():Array{
    var users:Array = saveDataObject.data.users as Array;
    return users;
}

public function saveUserInfo(name:String, pass:String):void{
    var users:Array = getUsers();
    if(!users){
        users = [];
    }
    users.push({username:name, password:pass});
    saveDataObject.data.users = users;
    saveDataObject.flush();
}

现在您可以围绕此编写各种其他功能。例如:

public function getPasswordForUser(username:String):String{
    var users:Array = getUsers();
    if(users){
        for (var i:int=0; i<users.length; i++){
            if(users[i].username == username){
                return users[i].password;
            }
        }
    }
    return null;
}

还有许多其他事情可以在这里完成,但这会让你前进