03-16-2003, 01:15 AM
|
#1
|
|
Registered User
Join Date: Feb 2003
Location: Richmond, VA
Posts: 68
|
list box action listener
I am using a list box to display a list of boats that i'm loading from an xml file at runtime. The way I have it set up now, is a button beneath the list box that gets the selected index and on the click, it displays information about the boat that is selected from the list. What i'd like to have happen is the details about the selection to display upon selecting it in the list box, thus not requiring the extra button. Any help would be greatly appreciated.
thanks
.c
|
|
|
03-16-2003, 01:34 AM
|
#2
|
|
Registered User
Join Date: Jan 2003
Posts: 397
|
very easy to do
look under the properties window. Under parameters you will find the word change handler.
create a function in actionscript and put the name of the function in this handler box (clickHandler) .
I have a listbox that loads pictures when clicked.
Here is an example of some handler actionscript:
function clickHandler(){
for(i=0;i<boatnames.length;i++){
if(textbox.getSelectedIndex == i){
// do something here with the item break;
}
}
}
|
|
|
03-16-2003, 01:56 AM
|
#3
|
|
Registered User
Join Date: Feb 2003
Location: Richmond, VA
Posts: 68
|
still not havin much luck
Here is the code I have on an action layer frame: What is the scope of the list box. Thats something i need to read up on. the listbox itself is on frame 3 as is all of this code, but its on a diffent layer. Everything good? So what do i need to make this work for me?
thanks again.
.c
code.......
stop();
UsedBoat_xml = new XML();
UsedBoat_xml.ignoreWhite = true;
// Setup load handler which just invokes another function
// which will do the parsing of our XML
UsedBoat_xml.onLoad = function(sucess) {
if (sucess) {processList(UsedBoat_xml);}
}
// Load up the XML file into Flash
UsedBoat_xml.load('UsedBoats.xml');
// This is the function that will be called when
// our XML document is loaded succesfully
function processList(List) {
// xmlDoc_xml is now a reference to the XML
// object where our information is stored
boat = new Object();
boat.Name = new Array();
boat.Manufacturer = new Array();
boat.Price = new Array();
boat.Length = new Array();
boat.Material = new Array();
Boat.Weight = new Array();
boat.Info = new Array();
boat.Photo = new Array();
for(i = 0; i < List.childNodes.length; i++){
boat.Name[i] = List.childNodes[i].childNodes[0].firstChild.nodeValue;
usedboats.addItem(boat.Name[i]);
boat.Price[i] = List.childNodes[i].childNodes[1].firstChild.nodeValue;
boat.Manufacturer[i] = List.childNodes[i].childNodes[2].firstChild.nodeValue;
boat.Length[i] = List.childNodes[i].childNodes[3].firstChild.nodeValue;
boat.Material[i] = List.childNodes[i].childNodes[4].firstChild.nodeValue;
boat.Weight[i] = List.childNodes[i].childNodes[5].firstChild.nodeValue;
boat.Info[i] = List.childNodes[i].childNodes[6].firstChild.nodeValue;
boat.Photo[i] = List.childNodes[i].childNodes[7].firstChild.nodeValue;
}//end for i
}//end function processList
function clickHandler()
{
index = usedboats.getSelectedIndex();
Description.gotoAndStop(2);
Description.Price.text = boat.Price[index];
Description.Manufacturer.text = boat.Manufacturer[index];
Description.Length.text = boat.Length[index];
Description.Material.text = boat.Material[index];
Description.Weight.text = boat.Weight[index];
Description.Info.text = boat.Info[index];
Description.Photo.gotoAndStop(boat.Photo[index]);//not available
}
|
|
|
03-16-2003, 12:24 PM
|
#4
|
|
Registered User
Join Date: Jan 2003
Posts: 397
|
use _global
I haven't studied the statement var obj = new Object(); so I'm not sure why you would need that. Maybe for xml, I haven't studied xml yet either.
But what I do know is if you use this to create an array or change a variable you may call it from any frame in the movie.
_global.boatname = new Array();
or
_global.boat = new Object;
I'm not sure if you would use _global before this statement.
I don't think you need to, but I'm not sure. When I use a normal _global variable and I want to change the value I use _global before it.
boat.Name[i] = List.childNodes[i].childNodes[1].firstChild.nodeValue;
I would like to learn how to use xml. I tried once but the browser only show the code. Do you have any examples I could try?
|
|
|
03-16-2003, 07:38 PM
|
#5
|
|
Master of Nothing
Join Date: Dec 2002
Location: San Diego, CA
Posts: 2,468
|
You are pretty close you just need to set changeHandler for the listbox. However I think you need to rethink your parser function. Right now you spread out data about a particular boat into 7 different arrays. You should think about encapsulating data for each boat in its own object. So you have an array of boats objects.
Code:
Variable _level0.boats = [object #169, class 'Array'] [
0:[object #170, class 'Object'] {
Name:"Boat 1",
Price:"100,000.00",
Manufacturer:"My Brand",
Length:"35'",
Material:"Fiberglass",
Weight:"300",
Info:"Some info",
Photo:"somephoto.jpg"
},
1:[object #171, class 'Object'] {
Name:"Boat 2",
Price:"100,000.00",
Manufacturer:"My Brand",
Length:"35'",
Material:"Fiberglass",
Weight:"300",
Info:"Some info",
Photo:"somephoto.jpg"
},
2:[object #172, class 'Object'] {
Name:"Boat 3",
Price:"100,000.00",
Manufacturer:"My Brand",
Length:"35'",
Material:"Fiberglass",
Weight:"300",
Info:"Some info",
Photo:"somephoto.jpg"
},
3:[object #173, class 'Object'] {
Name:"Boat 4",
Price:"100,000.00",
Manufacturer:"My Brand",
Length:"35'",
Material:"Fiberglass",
Weight:"300",
Info:"Some info",
Photo:"somephoto.jpg"
}
]
In order to do this you just need to change things around a bit. I have taken the liberty of writing some code for you. I have tested this and it works great.
ActionScript Code:
stop();
UsedBoat_xml = new XML();
UsedBoat_xml.ignoreWhite = true;
// Setup load handler which just invokes another function
// which will do the parsing of our XML
UsedBoat_xml.onLoad = function(sucess) {
if (sucess) {processList(UsedBoat_xml);}
}
// Load up the XML file into Flash
UsedBoat_xml.load('UsedBoats.xml');
// This is the function that will be called when
// our XML document is loaded succesfully
function processList(List)
{
boats = new Array();
var children = List.firstChild.childNodes;
for(i = 0; i < children.length; i++)
{
tmp = children[i].childNodes;
tmpObj = new Object();
for(j=0;j<tmp.length;j++)
{
if(tmp[j].nodeName == 'Name') {
usedboats.addItem(tmp[j].firstChild.nodeValue, j);
}
tmpObj[tmp[j].nodeName] = tmp[j].firstChild.nodeValue;
}
boats.push(tmpObj);
}//end for i
usedboats.setChangeHandler("clickHandler");
}//end function processList
function clickHandler()
{
index = usedboats.getSelectedIndex();
Description.gotoAndStop(2);
trace ("Gonna show info for " + boats[index].Name);
Description.Price.text = boats[index].Price;
Description.Manufacturer.text = boats[index].Manufacturer;
Description.Length.text = boats[index].Length;
Description.Material.text = boats[index].Material;
Description.Weight.text = boats[index].Weight;
Description.Info.text = boats[index].Info;
Description.Photo.gotoAndStop(boats[index].Photo);//not available
}
Provided your xml looks like
Code:
<?xml version="1.0" ?>
<boats>
<boat>
<Name>Boat 1</Name>
<Price>100,000.00</Price>
<Manufacturer>My Brand</Manufacturer>
<Length>35'</Length>
<Material>Fiberglass</Material>
<Weight>300</Weight>
<Info>Some info</Info>
<Photo>somephoto.jpg</Photo>
</boat>
<boat>
<Name>Boat 2</Name>
<Price>100,000.00</Price>
<Manufacturer>My Brand</Manufacturer>
<Length>35'</Length>
<Material>Fiberglass</Material>
<Weight>300</Weight>
<Info>Some info</Info>
<Photo>somephoto.jpg</Photo>
</boat>
<boat>
<Name>Boat 3</Name>
<Price>100,000.00</Price>
<Manufacturer>My Brand</Manufacturer>
<Length>35'</Length>
<Material>Fiberglass</Material>
<Weight>300</Weight>
<Info>Some info</Info>
<Photo>somephoto.jpg</Photo>
</boat>
<boat>
<Name>Boat 4</Name>
<Price>100,000.00</Price>
<Manufacturer>My Brand</Manufacturer>
<Length>35'</Length>
<Material>Fiberglass</Material>
<Weight>300</Weight>
<Info>Some info</Info>
<Photo>somephoto.jpg</Photo>
</boat>
</boats>
|
|
|
03-16-2003, 10:33 PM
|
#6
|
|
Registered User
Join Date: Feb 2003
Location: Richmond, VA
Posts: 68
|
wow, thanks a lot
Thanks very much for the code. You're right about the better implementation. Its sweet how different people see the same problem solution from two different ways. I've read through it, and it all makes sense. My guess is that it probably runs a little faster than my implementation. The in depth response is much appreciated! Thanks again.
.c
|
|
|
03-16-2003, 10:37 PM
|
#7
|
|
Master of Nothing
Join Date: Dec 2002
Location: San Diego, CA
Posts: 2,468
|
Hey no problem, I have been doing alot with OOP lately and data encapsulation is a big part of it. I just figured I could show you how to tighten the code a bit, glad it made sense.
|
|
|
03-21-2003, 01:24 PM
|
#8
|
|
Member
Join Date: Jul 2002
Location: Planet Rock
Posts: 74
|
Hi
I am currently working on something like this, actually I've been working the design( form before the function  my bad.)
but where exactly is the data being displayed?
I imagine in a dynamic Textfield but I tried for example to put a textfield and give it an instance name of Price or Description.
Quote:
|
Description.Price.text = boats[index].Price;
|
but to no avail.
Thanks.
|
|
|
03-21-2003, 05:16 PM
|
#9
|
|
Member
Join Date: Jul 2002
Location: Planet Rock
Posts: 74
|
ok I got it, I wasn't in an mc called descriptions...so I just took that off and bingo
Thanks for the great script... a little tweakin and it fits perfect
Cheers
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 05:12 PM.
///
|
|