0

我目前正在使用 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");
           }
      }

  }
}
4

1 回答 1

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;
}

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

于 2012-11-10T23:41:55.487 回答