Reputation: 105
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.
Upvotes: 0
Views: 190
Reputation: 2065
If you want to replace the text by the change of an InputField
you should listen to the Event.CHANGE
of the InputFields
. 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.
private var animalInputField:TextField;
private var cityInputField:TextField;
private var nameInputField:TextField;
private var xmlText:TextField;
public function Xml()
{
var xmlString:URLRequest = new URLRequest("tales.xml");
var xmlLoader:URLLoader = new URLLoader(xmlString);
xmlLoader.addEventListener(Event.COMPLETE, init);
function init(event:Event):void
{
xml = XML(xmlLoader.data);
xmlText = 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);
// create inputfields
animalInputField = createInputField();
cityInputField = createInputField();
nameInputField = createInputField();
cityInputField.x = 100;
nameInputField.x = 200;
updateTale();
}
}
private function createInputField() : TextField
{
var inputField:TextField = TextField(addChild(new TextField()));
inputField.type = TextFieldType.INPUT;
inputField.text = '...';
inputField.addEventListener(Event.CHANGE, onInputChanged);
return inputField;
}
private function onInputChanged(e:Event):void
{
updateTale();
}
private function updateTale() : void
{
var tale:String = xml.tale;
tale = tale.replace(/{ANIMAL}/g, animalInputField.text);
tale = tale.replace(/{CITY}/g, cityInputField.text);
tale = tale.replace(/{NAME}/g, nameInputField.text);
xmlText.text = tale;
}
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 xmlText
and xml
but never use them because within your init(event:Event)
listener you create two local vars called xmlText
and xml
aswell.
Upvotes: 1