Solved

In ActionScript 3, how do I use a combo box to get images from a XML file?

Posted on 2008-06-12
10
871 Views
Last Modified: 2013-11-11
Hello!

I am using a combo box to extract text and images from an XML file. I have the text part of the script working, but I've been unable to get the images to successfully come to stage. Any assistance is greatly appreciated.


//import controls

import flash.net.*;

import fl.controls.ComboBox;
 

//var declarations

var xmlData:XML;

var classDataLoader:URLLoader = new URLLoader();

var classreqDataLoader:URLLoader = new URLLoader();
 

//loading xml data

classDataLoader.load(new URLRequest("classes.xml"));

classDataLoader.addEventListener(Event.COMPLETE,loadCompleteClass);
 

//function extracting data from xml

function loadCompleteClass(event:Event):void 

{

	xmlData = new XML(classDataLoader.data);

	for (var i in xmlData.crclass) 

	{

		cbClasses.addItem({label:xmlData.crclass.name[i]});

		}

	}
 

//EventListeners for combo box

cbClasses.addEventListener(Event.CHANGE, showReq);
 

//function populating text area

function showReq(event:Event):void 

{

	var itemSelected:Number = cbClasses.selectedIndex;

	taReq.text = xmlData..requirement[itemSelected];

	}
 

/*XML code:

<classes>
 

	<crclass>

		<name>Class 5</name>

		<requirement>Bouffant Caps, Hoods, Beard Cover, Face Mask, Coveralls, Gloves, Shoe Covers, Booties</requirement>

		<figure>file = "class5.png" width = "144" height = "415"</figure>

	</crclass>

	

	<crclass>

		<name>Class 6</name>

		<requirement>Bouffant Caps, Hoods, Beard Cover, Face Mask, Coveralls, Gloves, Shoe Covers, Booties</requirement>

		<figure>file = "class6.png" width = "144" height = "415"</figure>

	</crclass>

	

	<crclass>

		<name>Class 7 (Cleanroom)</name>

		<requirement>Bouffant Caps, Hoods, Beard Cover, Face Mask, Coveralls, Gloves, Shoe Covers, Booties</requirement>

		<figure>file = "class7cr.png" width = "144" height = "415"</figure>

	</crclass>

	

	<crclass>

		<name>Class 7 (Benches)</name>

		<requirement>Bouffant Caps or Hoods, Beard Cover, Face Mask, Smock, Gloves</requirement>

		<figure>file = "class7bn.png" width = "144" height = "415"</figure>

	</crclass>

	

	<crclass>

		<name>Class 7 (Vestibules)</name>

		<requirement>Bouffant Caps or Hoods, Beard Cover or Face Mask, Smock, Gloves</requirement>

		<figure>file = "class7vs.png" width = "144" height = "415"</figure>

	</crclass>

	

	<crclass>

		<name>Class 8</name>

		<requirement>Bouffant Caps, Beard Cover, Smock</requirement>

		<figure>file = "class8.png" width = "144" height = "415"</figure>

	</crclass>

	

</classes>

*/

Open in new window

0
Comment
Question by:ColdSpringProductions
  • 5
  • 4
10 Comments
 
LVL 5

Expert Comment

by:cwickens
Comment Utility
code looks ok.

 I would say that you should make sure that the SWF, XML and images are all in the same directory since that is how you are calling it...
0
 

Author Comment

by:ColdSpringProductions
Comment Utility
Well, the problem I'm having is that I can populate the text area okay, but I can't seem to figure out how to call the .pngs to the stage when a selection is made in the combo box. That's where I've been beating out my brains for the past two weeks.
0
 
LVL 5

Expert Comment

by:cwickens
Comment Utility
ok, it doesn't look like you are loading the images at all. I was just looking at the directory structure, since I always screw myself up with that...  sorry about that.  But just to be safe, double check yours!  Anyhow, try this.

Add a movieclip to the stage, give it an instance name of mySquare

add this to the showReq function:
loadMovie(xmlData..figure[itemSelected], mySquare);

so it looks like this:

function showReq(event:Event):void
{
        var itemSelected:Number = cbClasses.selectedIndex;
        taReq.text = xmlData..requirement[itemSelected];
        loadMovie(xmlData..figure[itemSelected], mySquare);
        }

That should be close.  Let me know if it works
0
 

Author Comment

by:ColdSpringProductions
Comment Utility
Okay, I got two errors:
1046: Type was not found or was not a compile-time constant: mySquare
1180: Call to a possibly undefined method loadMovie
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ColdSpringProductions
Comment Utility
I changed the properties on mySquare in that I deselected the Export for ActionScript option in Linkage.

Now I'm left with the 1180 error.
0
 
LVL 5

Expert Comment

by:cwickens
Comment Utility
my appologies,  loadMovie() is for AS2.  You need the loader() class.  

I will see if I can sort out the function and post it a bit later, I am walking out the door to a meeting and will be back on later.
0
 

Author Comment

by:ColdSpringProductions
Comment Utility
Any luck on finding how to use the loader() class for this case?
0
 
LVL 5

Accepted Solution

by:
cwickens earned 500 total points
Comment Utility
wow, I dropped the ball big time here, sorry about that.  Since you are using images that are all the same size, create a movieclip that is H=415 and W=144.  Then change the <figure> tags from this:
<figure>file = "class5.png" width = "144" height = "415"</figure>
to this:
<figure>class5.png</figure>

//Create loader instance.
var my_ldr:Loader = new Loader();

//add this to your showReq function:

function showReq(event:Event):void
{
        var itemSelected:Number = cbClasses.selectedIndex;
        taReq.text = xmlData..requirement[itemSelected];
        my_ldr.load(xmlData..figure[itemSelected]); //<-- add this line
        }


Let me know if that helps...if it doesn't, can you upload your fla, xml and images (you will need to change the file extension of the FLA to .TXT or .XLS or something in order to post it) and I will see what I can do.

sorry it took so long to get back to you,
c
0
 

Author Comment

by:ColdSpringProductions
Comment Utility
No worries--I'll give it a try tomorrow.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
D3, Javascript, SVG, HTML canvas, div 2 91
SQL Remove Elements from XML 6 39
Problem to event 3 47
C# SQL BULK INSERT CLASS 5 33
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to how to record live broadcast.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now