AlexPIMP
April 14th, 2008, 10:21 AM
Hello everybody,
Task: I want to enter a message in input text field and write it in the dynamic using a custom event dispatching.
Solution:
I have 2 textfields on the stage.
One textfield is an input text field the other is a dynamic text field which will server just to display text.
on the flash in the first frame I made this code:
var messageBoard:MsgBoard = new MsgBoard(mb); // mb is the instance name of the dynamic text field already placed on the stage
var user1:UserInput = new UserInput(u1); // u1 is the input text field placed on the stageAlso I wrote 3 very simple classes.
1. UserInput.as
package {
import flash.text.TextField;
import flash.ui.Keyboard;
import flash.events.*;
public class UserInput extends EventDispatcher{
private var textInput:TextField;
public function UserInput(ti:TextField){
textInput = ti;
//listening user input in the input text
textInput.addEventListener(KeyboardEvent.KEY_UP, send_event);
}
private function send_event(evnt:KeyboardEvent):void{
//once enter pressed dispatching our custom event
if(evnt.keyCode == Keyboard.ENTER){
var msg:MsgEvent = new MsgEvent(textInput.text);
this.dispatchEvent(msg);
textInput.text = '';
}
}
}
}2. MsgEvent.as
package {
import flash.events.Event;
public class MsgEvent extends Event{
public static const NEW:String = "new_message";
public var msg:String;
public function MsgEvent(str:String){
super(NEW);
msg = str;
}
public override function clone():Event
{
return new MsgEvent(msg);
}
}
}3. MsgBoard.as
package {
import flash.text.TextField;
import flash.display.Sprite;
public class MsgBoard extends Sprite{
private var displayTextArea:TextField;
public function MsgBoard(mb:TextField){
displayTextArea = mb;
displayTextArea.text = 'listening to the user input';
// as displayTextArea is a reference to the object that is located on the Stage
// it should have an ability to listen to the events as it's a part of event flow
displayTextArea.addEventListener(MsgEvent.NEW, refreshTextArea);
}
private function refreshTextArea(evnt:MsgEvent):void{
displayTextArea.appendText(evnt.msg);
}
}
}
Problem: Somehow it doesn't work. I actually made it work by making a listener the same object that dispatches the event.
But I want to understand why it doesn't works the way I showed above. I browsed a lot of forums and found that all the
people use to listen by the same object that is dispatching. I think it's like talking with yourself isn't it?
Task: I want to enter a message in input text field and write it in the dynamic using a custom event dispatching.
Solution:
I have 2 textfields on the stage.
One textfield is an input text field the other is a dynamic text field which will server just to display text.
on the flash in the first frame I made this code:
var messageBoard:MsgBoard = new MsgBoard(mb); // mb is the instance name of the dynamic text field already placed on the stage
var user1:UserInput = new UserInput(u1); // u1 is the input text field placed on the stageAlso I wrote 3 very simple classes.
1. UserInput.as
package {
import flash.text.TextField;
import flash.ui.Keyboard;
import flash.events.*;
public class UserInput extends EventDispatcher{
private var textInput:TextField;
public function UserInput(ti:TextField){
textInput = ti;
//listening user input in the input text
textInput.addEventListener(KeyboardEvent.KEY_UP, send_event);
}
private function send_event(evnt:KeyboardEvent):void{
//once enter pressed dispatching our custom event
if(evnt.keyCode == Keyboard.ENTER){
var msg:MsgEvent = new MsgEvent(textInput.text);
this.dispatchEvent(msg);
textInput.text = '';
}
}
}
}2. MsgEvent.as
package {
import flash.events.Event;
public class MsgEvent extends Event{
public static const NEW:String = "new_message";
public var msg:String;
public function MsgEvent(str:String){
super(NEW);
msg = str;
}
public override function clone():Event
{
return new MsgEvent(msg);
}
}
}3. MsgBoard.as
package {
import flash.text.TextField;
import flash.display.Sprite;
public class MsgBoard extends Sprite{
private var displayTextArea:TextField;
public function MsgBoard(mb:TextField){
displayTextArea = mb;
displayTextArea.text = 'listening to the user input';
// as displayTextArea is a reference to the object that is located on the Stage
// it should have an ability to listen to the events as it's a part of event flow
displayTextArea.addEventListener(MsgEvent.NEW, refreshTextArea);
}
private function refreshTextArea(evnt:MsgEvent):void{
displayTextArea.appendText(evnt.msg);
}
}
}
Problem: Somehow it doesn't work. I actually made it work by making a listener the same object that dispatches the event.
But I want to understand why it doesn't works the way I showed above. I browsed a lot of forums and found that all the
people use to listen by the same object that is dispatching. I think it's like talking with yourself isn't it?