Quote:
Below is the modified code that I'm having problems with.
I'm still not able to replicate. Did you try the code in my last post? It works in my testing. Make sure that your text field is a dynamic text field. If you still can't get it to work, post your .fla.
Quote:
My preference would be not to have "title" as a variable at all in the flash file and not have title="" in the xml file.
Again, the code I provided in my last post should work. If it doesn't, then something might be up with your setup.
The below code gets rid of ALL references to the title. The code I posted earlier does the same thing, but I kept the old code commented out. I guess that's what you were getting confused with.
Code:
// Create a URLLoader instance to load a external file
var loader:URLLoader = new URLLoader();
// When the URLLoader completes loading our XML file into the Flash movie.
// fire the showRandomQuote() function. This is the replacement for AS2's onLoad() method
loader.addEventListener(Event.COMPLETE, showRandomQuote);
// Create the XML Object and initiate the XML() Class.
var xml:XML;
function showRandomQuote(e:Event):void {
// Create a new XML object
xml = new XML(e.target.data);
// Create a XMLList object that holds all of our quotes (think of it as an array)
var quote:XMLList = xml.quote
// Here we calculate the number of quotes there are in the XML file and populate the
// totalQuotes variable.
var index:Number = Math.floor(Math.random() * (quote.length() - 0));
// Set theAuthor text field to the contents of the author attribute in the random node
theAuthor.text = quote[index].attribute("author");
// Set theQuote text area to the contents of the quote node in the random node
theQuote.text = quote[index];
}
// Don't forget to load the actual XML file.
// To load the XML file we simply use the .load method of the URLLoader class
// In AS3, all external requests must be made by an URLRequest Object
loader.load(new URLRequest("quotes.xml"));
Just delete the title="" attributes from the XML. Also, delete the theTitle text input from the stage.
Quote:
Also, any idea why the link to the modified xml file wouldn'l link just quotes_av.xml but would work as ./quotes_av.xml while they are all in the same directory??
Sounds like server issues. Are you using any type of .htaccess directive?
Quote:
Is there a way to put a line break between the actual quote and the author insted of having two text boxes as the quotes vary in length.
Yeah. Try the code below. Of course, you'll want to remove theTitle and theAuthor text fields first.
Code:
// Create a URLLoader instance to load a external file
var loader:URLLoader = new URLLoader();
// When the URLLoader completes loading our XML file into the Flash movie.
// fire the showRandomQuote() function. This is the replacement for AS2's onLoad() method
loader.addEventListener(Event.COMPLETE, showRandomQuote);
// Create the XML Object and initiate the XML() Class.
var xml:XML;
function showRandomQuote(e:Event):void {
// Create a new XML object
xml = new XML(e.target.data);
// Create a XMLList object that holds all of our quotes (think of it as an array)
var quote:XMLList = xml.quote
// Here we calculate the number of quotes there are in the XML file and populate the
// totalQuotes variable.
var index:Number = Math.floor(Math.random() * (quote.length() - 0));
// Add the author's name to the top of the text area
theQuote.text = quote[index].attribute("author");
// Add a line break
theQuote.appendText("\r\n");
// Set theQuote text area to the contents of the quote node in the random node
theQuote.appendText(quote[index]);
}
// Don't forget to load the actual XML file.
// To load the XML file we simply use the .load method of the URLLoader class
// In AS3, all external requests must be made by an URLRequest Object
loader.load(new URLRequest("quotes.xml"));
With the current setup though, there is no benefit for doing this. And it won't solve the problem of the quote having variable height. Assuming, that your authors' names do not go onto a second line, the author's line will always be the same height. So, simply adding it into the same textarea won't solve much.
The code you're looking for is:
Code:
theQuote.autoSize = TextFieldAutoSize.LEFT;
This will allow theQuote to expand/contract to whatever size the text within it is. You don't need to have everything in the same text box. Though, doing so won't have any detrimental (or beneficial) effects.