PDA

View Full Version : Custom Events Dispatching



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?

wvxvw
April 14th, 2008, 12:45 PM
First: if you placed something on the stage it's the same as calling new YuorClass(). So, either use first method, or second - both will most probably give an error.
Here' a very simple example of what you ware going to do:

var in_txt:TextField = new TextField();
var out_txt:TextField = new TextField();
in_txt.type = 'input';
in_txt.border = out_txt.border = true;
in_txt.width = out_txt.width = 200;
in_txt.height = out_txt.height = 20;
out_txt.y = 25;
in_txt.addEventListener(TextEvent.TEXT_INPUT, handleInput);
function handleInput(evt:TextEvent):void {
out_txt.appendText(evt.text);
}
addChild(in_txt);
addChild(out_txt);