I have a inputTextField, and in my inputTextField in my GamePage class i want to be able to take the text that you should be able to write in the inputTextField, and replace the text from the inputTextField with my nodes in my xml-fil. In my Xml class above I want to have that code that converts the text from the inputTextField, ex. If i write “monkey” in my inputTextField, I want to replace {ANIMAL} node in my xml-fil with monkey.
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.xml.XMLDocument;
import flash.xml.XMLNode;
public class Xml extends Sprite
{
private var xmlText:TextField;
private var xml:XML;
private var textBox:TextField;
public function Xml()
{
var xmlString:URLRequest = new URLRequest("tales.xml");
var xmlLoader:URLLoader = new URLLoader(xmlString);
xmlLoader.addEventListener("complete", init);
function init(event:Event):void
{
var xml:XML = XML(xmlLoader.data);
var xmlText:TextField = new TextField();
addChild(xmlText);
xmlText.width = 500; xmlText.height = 500; xmlText.x = 5; xmlText.y = 100;
xmlText.text = xml.tale; XML.ignoreWhitespace = true;
var format:TextFormat = new TextFormat();
format.color = 0x990000;
format.size = 18;
xmlText.setTextFormat(format);
}
}
}
}
This is my xml-fil (Google Translated from Swedish):
There was once a {ANIMAL} who came from {CITY}. {ANIMAL} lived in a small little red julhus, just like Santa does. {CITY} also had her Santa Claus, and his name {NAME}. {ANIMAL} and {NAME} was in fact best friends for ages ago, and they lived in the same little red house.
If you want to replace the text by the change of an
InputFieldyou should listen to theEvent.CHANGEof theInputFields. Within this listener you can redirect to a method which will do the replacement. After loading the XML you should also call this method so the initial values are stripped out.I didn’t see the InputFields in your code example so here’s a quick example of how you could set it up.
Within the
updateTale()method I’m using a regular expression where I use the ‘g’ (global) flag to make Flash search to any instance instead of only the first occurrence.Please note that in your example you have a private var
xmlTextandxmlbut never use them because within yourinit(event:Event)listener you create two local vars calledxmlTextandxmlaswell.