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 : )

Member Avatar for iamthwee

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>
commented: Not the first to give correct answer, but very hekpful thanks again +2
Member Avatar for rajarajan2017

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

Member Avatar for rajarajan2017

I looked into your code and executed the half, but your method of total is perfectly working.

Member Avatar for rajarajan2017

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;

commented: good spot +22
commented: Nicely spotted! +2
commented: Very precise correct answer, thanks again +2
Member Avatar for iamthwee

Yep post #5 was the correct answer, or should I say the shortest answer/fix.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.