0

Hola, Im an experienced coder, however new to java/actionscript 3

I am trying to make a very simple login and I am running into problems. They seem to be with my for loop with the .Length() function in particular...

stop();
loginError.visible = false;
var loginXml:XML;
var loader:URLLoader = new URLLoader(); 
loader.load(new URLRequest("logindata.xml"));
loader.addEventListener(Event.COMPLETE, loadXML);
var loginData = new Array();
var temp:Array = new Array();
function loadXML(e:Event):void
{
    loginXml = new XML(e.target.data);
	var total = loginXml.item.length();
	var x:int = 0;
	for(x=0; x < total; x++)
	{
		loginData[x] = new Array();
		loginData[x][0] = loginXml.item[x].user.toString();
		loginData[x][1] = loginXml.item[x].pass.toString();
	}
	
}
loginBtn.addEventListener(MouseEvent.CLICK, loginFunction2);

function loginFunction2(e:MouseEvent):void
{
	var success:Boolean = false;
    //SEEMS TO BE WHERE MY ERROR STARTS
    var lengthLD:int = loginData.length();
	for(var i=0; i < lengthLD; i++)                   //Most Likley the Error
	{
		if(user.text == loginData[i][0] && pass.text == loginData[i][1])
		  {
			  success = true;
			  gotoAndStop(user.text);
		  }
	}
	if(!success)
	{
		loginError.visible = true;
	}
}

Any help would be great,
Thanks : )

3
Contributors
6
Replies
8
Views
8 Years
Discussion Span
Last Post by DevC++4.9.9.2
0

I'd just make a global variable

var loginXml:XML;
var loader:URLLoader = new URLLoader(); 
loader.load(new URLRequest("logindata.xml"));
loader.addEventListener(Event.COMPLETE, loadXML);
var loginData = new Array();
var temp:Array = new Array();


var gtotal:int = 0;

function loadXML(e:Event):void
{
    loginXml = new XML(e.target.data);
	gtotal = loginXml.item.length();	
                var x:int = 0;
	for(x=0; x < gtotal; x++)
	{
		loginData[x] = new Array();
		loginData[x][0] = loginXml.item[x].user.toString();
		loginData[x][1] = loginXml.item[x].pass.toString();
		
		//trace it out
		//trace(loginData[x][1]);
		
		
	}
	
	//trace(gtotal);
	
}

button1.addEventListener(MouseEvent.CLICK, go);
function go(e:MouseEvent)
{
  var success:Boolean = false;

   
	for(var i=0; i < gtotal; i++)                   //Most Likley the Error
	{
		if(user.text == loginData[i][0] && pass.text == loginData[i][1])
		  {
			  success = true;
			  //gotoAndStop(user.text);
		  }
	}
	if(!success)
	{
		//loginError.visible = true;
	}
	
	trace(success);

}

my xml file:

<root>
  <item>
    <user>john</user>
    <pass>password</pass>
  </item>
  <item>
    <user>sally</user>
    <pass>hello</pass>
  </item>
</root>
Votes + Comments
Not the first to give correct answer, but very hekpful thanks again
0

In AS 3.0, its very simple to find the length.

Step 1:

Use this:

var loginform:XMLList;
var total:Number=0;

instead of :

var gtotal:int = 0;

Step 2:

use this:

total = loginform.length();

instead of:

gtotal = loginXml.item.length();

Sure It will work, if yes, mark as solved. Thanks

3

Mr. DevC++4.9.9.2,

I am so sorry I am confused forget all those things said above, that is also correct, but in your program nothing is wrong except you have to remove two parenthesis in length function, thats it done.

var lengthLD:int = loginData.length;

Votes + Comments
Very precise correct answer, thanks again
Nicely spotted!
good spot
0

Thank you very much... This answer worked... I am coming from c++ and these little things are ganna kill me lol

My final code:

stop();
loginError.visible = false;
var loginXml:XML;
var loader:URLLoader = new URLLoader(); 
loader.load(new URLRequest("logindata.xml"));
loader.addEventListener(Event.COMPLETE, loadXML);
var loginUser:Array = new Array();
var loginPass:Array = new Array();
var total:Number = 0;
function loadXML(e:Event):void
{
    loginXml = new XML(e.target.data);
	total = loginXml.length();
	var x:Number = 0;
	for(x=0; x < total; x++)
	{
		loginUser.push(loginXml.item[x].user.toString());
		loginPass.push(loginXml.item[x].pass.toString());
	}
}
loginBtn.addEventListener(MouseEvent.CLICK, loginFunction);

function loginFunction(e:MouseEvent):void
{
	var success:Boolean = false;
	var i:Number = 0;
	for(i=0; i < total; i++)
	{
		if(user.text == loginUser[i] && pass.text == loginPass[i])
		  {
			  success = true;
			  gotoAndStop(user.text);
		  }
	}
	if(!success)
	{
		loginError.visible = true;
	}
}

thanks again

In AS 3.0, its very simple to find the length.

Step 1:

Use this:

var loginform:XMLList;
var total:Number=0;

instead of :

var gtotal:int = 0;

Step 2:

use this:

total = loginform.length();

instead of:

gtotal = loginXml.item.length();

Sure It will work, if yes, mark as solved. Thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.