Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-16-2003, 01:15 AM   #1
vcuchris
Registered User
 
Join Date: Feb 2003
Location: Richmond, VA
Posts: 68
Send a message via AIM to vcuchris
Question 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
vcuchris is offline   Reply With Quote
Old 03-16-2003, 01:34 AM   #2
yorkeylady
Registered User
 
Join Date: Jan 2003
Posts: 397
Send a message via ICQ to yorkeylady Send a message via AIM to yorkeylady Send a message via Yahoo to yorkeylady
Default 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;
}
}
}
yorkeylady is offline   Reply With Quote
Old 03-16-2003, 01:56 AM   #3
vcuchris
Registered User
 
Join Date: Feb 2003
Location: Richmond, VA
Posts: 68
Send a message via AIM to vcuchris
Angry 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
}
vcuchris is offline   Reply With Quote
Old 03-16-2003, 12:24 PM   #4
yorkeylady
Registered User
 
Join Date: Jan 2003
Posts: 397
Send a message via ICQ to yorkeylady Send a message via AIM to yorkeylady Send a message via Yahoo to yorkeylady
Default 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?
yorkeylady is offline   Reply With Quote
Old 03-16-2003, 07:38 PM   #5
freddycodes
Master of Nothing
 
Join Date: Dec 2002
Location: San Diego, CA
Posts: 2,468
Default

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>
freddycodes is offline   Reply With Quote
Old 03-16-2003, 10:33 PM   #6
vcuchris
Registered User
 
Join Date: Feb 2003
Location: Richmond, VA
Posts: 68
Send a message via AIM to vcuchris
Talking 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
vcuchris is offline   Reply With Quote
Old 03-16-2003, 10:37 PM   #7
freddycodes
Master of Nothing
 
Join Date: Dec 2002
Location: San Diego, CA
Posts: 2,468
Default

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.
freddycodes is offline   Reply With Quote
Old 03-21-2003, 01:24 PM   #8
djungle
Member
 
Join Date: Jul 2002
Location: Planet Rock
Posts: 74
Default

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.
djungle is offline   Reply With Quote
Old 03-21-2003, 05:16 PM   #9
djungle
Member
 
Join Date: Jul 2002
Location: Planet Rock
Posts: 74
Default

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
djungle is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:28 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.