PDA

View Full Version : need some as3 guidence



wdaniel
12-23-2008, 10:29 PM
first of all i have a xml file with names and phone numbers. what im trying to do is set up an input text field to put in a name and when you hit submit i want the dynamic text field to show a phone number. im extremely lost and need help. im using flash cs3 as3.


please please help me:confused::confused::confused::confused::bow:bow:bow



w............

JonO
01-06-2009, 10:52 AM
Hi wdaniel,

Assuming your xml file is setup something like this:
<people>
<person>
<name>John Doe</name>
<phone>1234567</phone>
</person>
<person>
<name>Jane Doe</name>
<phone>9876543</phone>
</person>
</people>

You'd use something like the following to read in the xml data:

var urlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

urlLoader.addEventListener(Event.COMPLETE, onLoadComplete);

urlLoader.load(new URLRequest("http://your.xml.file.location.xml"));

function onLoadComplete(e:Event):void {

xmlData = new XML(e.target.data);

}

At this point you have your xml file loaded into the xmlData variable. Note that you can do this each time the user clicks the button, or you can load it at startup which is probably a better solution depending on how large your xml file is.

I'm assuming you have your text field and input button already in place and know how to call a function when you click your submit button. Assuming you loaded the xml file at startup, all that's left is to parse the xml data for the person's name and display the corresponding phone number in the text field:

Assuming the id of input field for the person's name is txtName and the id of the output field for the phone number is txtPhone, on the click event of the submit button you'd call the findPhoneNumber function and pass the value of txtName.text, like so:

findPhoneNumber(txtName.text);

function findPhoneNumber(var searchName:String):void
{
var personDetails:XMLList = xmlData.person.(name == searchName);
txtPhone.text = personDetails.phone;
}

That should get you going,
Cheers,
Jon

wdaniel
01-06-2009, 03:35 PM
thank you so much, i will play with it tonight. im sure i will have more ???????? if you don't mind.

w..................

wdaniel
01-06-2009, 10:19 PM
i get errors, i don't know whats wrong.

I'm using the .xml that you gave me i named it num.xml


submit button instance name is submit_btn
input box instance name is txtName
dynamic text box instance name is txtPhone







var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("num.xml"));

function LoadXML(e:Event):void {

xmlData = new XML(e.target.data);


}
submit_btn.addEventListener(MouseEvent.CLICK, findPhoneNumber);
findPhoneNumber(txtName.text)
function findPhoneNumber(var searchName:String):void {

var personDetails:XMLList = xmlData.person.(name == searchName);
txtPhone.text = personDetails.phone;

}

JonO
01-07-2009, 09:25 AM
Looking at your code I see a couple of problems here:

submit_btn.addEventListener(MouseEvent.CLICK, findPhoneNumber);
findPhoneNumber(txtName.text)

First in the line: submit_btn.addEventListener(MouseEvent.CLICK, findPhoneNumber);
When you call a function through an event listener like this, the function that is referenced is automatically passed the corresponding event, so in this case the function findPhoneNumber is being called and is automatically being passed the click event. However the function is expecting to receive a variable of type string, not type Event. If you want to directly call the findPhoneNumber function from the click event you will need to change the function so that it accepts a variable of type Event, but since you will no longer be passing the name via a parameter to the function and cannot directly access the properties of txtName from within findPhoneNumber you will also need to add code to update a global variable with the value of txtName.text by adding an event listener to it's change event:

var searchName:String = "";
txtName.addEventListener(Event.CHANGE, onNameChange);

function onNameChange(var e:Event):void
{
searchName = e.currentTarget.text;
}

function findPhoneNumber(var e:Event):void
{
var personDetails:XMLList = xmlData.person.(name == searchName);
txtPhone.text = personDetails.phone;
}

Also since you are calling the findPhoneNumber function directly from the click event of the submit button, there is no need for the line "findPhoneNumber(txtName.text)".

If you have any more errors please include them with your post so that we can better help you.

Cheers,
Jon

wdaniel
01-07-2009, 10:27 PM
now im confused (sorry) i will work through this.




i'v gotten this far. the parsing works and i can trace each step of the xml file.

my three boxes submit_btn,txtName,txtPhone are still the same.


var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("num.xml"));

function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
ParseData(xmlData);
}

function ParseData(people:XML):void {


trace(people.person.phone.text());

}






submit_btn.addEventListener(MouseEvent.CLICK); ??? what else do i need here ???



this is what i tried based on the above, no go






var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("num.xml"));

function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
ParseData(xmlData);
}

function ParseData(people:XML):void {

submit_btn.addEventListener(MouseEvent.CLICK, findPhoneNumber);

var searchName:String = "";
txtName.addEventListener(Event.CHANGE, onNameChange);

function onNameChange(var e:Event):void
{
searchName = e.currentTarget.text;
}

function findPhoneNumber(var e:Event):void
{
var personDetails:XMLList = people.person.(name == searchName);
txtPhone.text = personDetails.phone;
}




}





errors, at this point dosen't matter because i know its wrong

wdaniel
01-10-2009, 07:09 PM
OK I'm still trying and noting, can someone please do up an example for me before i go batty. i would really appreciate it..once i see a working example i think i will understad it better.:bow:bow:bow:bow:bow:bow:bow:bow:bow:bow


w..............

JonO
01-11-2009, 06:13 PM
hi wdaniel,

It looks like you're getting close. Rather than writing an example from scratch, if you can attach your full flash project then we should be able to debug and repost it back to you.

wdaniel
02-16-2009, 09:29 PM
does any one have a tutorial or know of one, this is just pissing me off it seem so simple but but nothing works. i have spent every night scene my last post pondering this and nothing works. JonO my project is useless what I've written above is my is my project. also a cant seem to get zip files or flash files to upload to thread.

please please please please please please please please please someone show me were theirs tutorials or something that can help me. I've been to three book stores and countless hour surfing and can't find an exact example to help me.

all i want is a simple search function where you put in a name and it gives you a phone number. useing as3 and xml


w..........

JonO
02-17-2009, 12:46 AM
wdaniel -

I've attached a sample file that does what you needed (reads in an external xml file containing a list of people's names and phone numbers and displays the phone number based on a name typed into a text box). The code is very similar to what we've discussed here previously, but I'll give a quick overview here:

Components on stage: 2 text boxes (txtName, txtPhone) and 1 button (submitButton).

AS3:

//Add the click event listener to the submitButton. When clicked, call //onButtonClick
submitButton.addEventListener(MouseEvent.CLICK, onButtonClick);

//Create the variable xmlData for later use
var xmlData:XML;


//This function reads in the xml file. Once complete, the urlLoader calls the //onLoadComplete function
function onButtonClick(e:MouseEvent):void
{
//First read in the xml file
var urlLoader:URLLoader = new URLLoader();
xmlData = new XML();

urlLoader.addEventListener(Event.COMPLETE, onLoadComplete);

urlLoader.load(new URLRequest("phonelist.xml"));
}

//This function assigns the data from the urlLoader to the xmlData variable and //then calls the findPhoneNumber function
function onLoadComplete(e:Event):void
{
xmlData = new XML(e.target.data);
findPhoneNumber(txtName.text);
}


//This function parses the xmlData and if it finds a match in the <pname> //attribute, it updates txtPhone's text property with the correct phone number
function findPhoneNumber(searchName:String):void
{
var personDetails:XMLList = xmlData.person.(pname == searchName);
txtPhone.text = personDetails.phone;
}

The attached zip file contains the flash file, sample xml file, and compiled swf, if you have any further questions or problems let us know.

Cheers,
Jon

wdaniel
02-17-2009, 09:42 PM
thank you so much, it works. now because i really want understand and not just take your work and run. another question. below is something i got online, now if you look at the xml loader stuff is before the functions. i guess my question is why in some examples ive seen is all the xml stuff done before the functions and submit buttons. but, in yours you have submit buttin first and the xml thrown in a function. is this what you ment when you quoted before
"Note that you can do this each time the user clicks the button, or you can load it at startup which is probably a better solution depending on how large your xml file is."



var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();


xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("Book.xml"));

function LoadXML(e:Event):void {

xmlData = new XML(e.target.data);
ParseBooks(xmlData);

}

function ParseBooks(bookInput:XML):void {

trace("XML Output");
trace("------------------------");

var bookList:XMLList = bookInput.Book.(@ISBN == "0743273567").title;



trace(bookList);
textfield.text = bookList;



}



--------------------------------------------------------

submitButton.addEventListener(MouseEvent.CLICK, onButtonClick);
var xmlData:XML;

function onButtonClick(e:MouseEvent):void
{
//First read in the xml file
var urlLoader:URLLoader = new URLLoader();
xmlData = new XML();

urlLoader.addEventListener(Event.COMPLETE, onLoadComplete);

urlLoader.load(new URLRequest("phonelist.xml"));
}

function onLoadComplete(e:Event):void
{
xmlData = new XML(e.target.data);
findPhoneNumber(txtName.text);
}


function findPhoneNumber(searchName:String):void
{
var personDetails:XMLList = xmlData.person.(pname == searchName);
txtPhone.text = personDetails.phone;
}

JonO
02-18-2009, 05:01 PM
yep that's exactly right. In the example you sent the the xml loading does not require a mouse click to work, it pulls the data as soon as that actionscript file is run. This is probably a better way since the xml file will probably not be updated regularly enough to warrant pulling the data in fresh every time you do a search.

wdaniel
02-25-2009, 07:49 AM
ok new problem, as3 is killing me, but i do see benefits. how would i change this to as3.

//left arrow


stop();

var myArray = new Array();
myArray[0] = "Main Menu \nSpeed dial feature ?";
myArray[1] = "Main Menu \nMore features ?";
myArray[2] = "Main Menu \nView active features ?";
myArray[3] = "Main Menu \nCall Log ?";
myArray[4] = "Main Menu \nProgram/Service ? ";
myArray[5] = "Main Menu \nPhone settings ? ";
myArray[6] = "Main Menu \nExit ? ";
myNumber = 6;





myText = myArray[myNumber = 6];

left_btn.onRelease = function() {
// Subtract 1 from myNumber
myNumber = myNumber-1;
myText = myArray[myNumber];
if (myNumber < 0) {
// Display the following
myText = myArray[myNumber = 6];
}

};

right_btn.onRelease = function() {
// Subtract 1 from myNumber
myNumber = myNumber+1;
myText = myArray[myNumber];
if (myNumber > 6) {
// Display the following
myText = myArray[myNumber = 0];
}

};



check_btn.onRelease = function() {
if (myText == myArray[6]) {
// Display the following
gotoAndPlay (1);
}

if (myText == myArray[0]) {
// Display the following
gotoAndPlay (5);

}

};

wdaniel
02-26-2009, 09:59 PM
any one as2 to as3 help


here is what i have, the trace works ok but i get a text() non-null error

stop();




var myArray:Array = new Array();
myArray[0] = "Main Menu \nSpeed dial feature ?";
myArray[1] = "Main Menu \nMore features ?";
myArray[2] = "Main Menu \nView active features ?";
myArray[3] = "Main Menu \nCall Log ?";
myArray[4] = "Main Menu \nProgram/Service ? ";
myArray[5] = "Main Menu \nPhone settings ? ";
myArray[6] = "Main Menu \nExit ? ";
var myNumber:Number = 6;

myText.text = myArray[myNumber = 6];

trace (myArray);
right_btn.addEventListener(MouseEvent.CLICK,rClick);
function rClick(event:MouseEvent):void{

trace (myArray);


// Subtract 1 from myNumber
myNumber = myNumber+1;
myText.text = myArray[myNumber];
if (myNumber > 6) {
// Display the following
myText.text = myArray[myNumber = 0];
}

};