I am new at flash, the below is my script, i have 3 textinput boxes, name1, name2, name3 and 3 dynamic texts, output1, output2, 开发者_高级运维output3. Once the user, enters the text in the box, it should appear exactly the same in the dynamic output text. It works for the first one, but does not work for the second and third one. I renamed changehandlers differently to remove compile errors, but now only the first one works. Is there a better way of doing this, if i want to have multiple textbox entrees?
name1.addEventListener(Event.CHANGE, changeHandler);
function changeHandler(e:Event):void
{
output1.text = name1.text
}
name2.addEventListener(Event.CHANGE, changeHandler);
function changeHandler1(e:Event):void
{
output2.text = name2.text;
}
name3.addEventListener(Event.CHANGE, changeHandler);
function changeHandler2(e:Event):void
{
output3.text = name3.text;
}
You forgot to change the name of the listener functions in the latter two addEventListener()
calls. It currently calls changeHandler()
on all three events.
You should have:
name2.addEventListener(Event.CHANGE, changeHandler1);
name3.addEventListener(Event.CHANGE, changeHandler2);
You can create a class that manages joining the input text field with the output text field:
package
{
import flash.text.TextField;
import flash.events.Event;
public class TextBinder extends Object
{
// vars
private var _input:TextField;
private var _output:TextField;
/**
* Joins input with output
* @param inp The input text field
* @param outp The output text field
*/
public function join(inp:TextField, outp:TextField):void
{
_input = inp;
_output = outp;
_input.addEventListener(Event.CHANGE, _change);
}
/**
* Event.CHANGE
*/
private function _change(e:Event):void
{
_output.text = _input.text;
}
}
}
And now you can loop through your text fields and join them using this:
var tb:TextBinder = new TextBinder();
tb.join(name1, output1);
精彩评论