Hello Guys, I created an application that whenever I click the button, the text on the textfield will change, I tried to use dispatch Event in this…
Here is my Code:
Main Class:
package {
import flash.display.MovieClip;
public class Main extends MovieClip
{
private var _content:Content;
private var _btn:CustomButtons;
public function Main():void
{
createContent();
createButton();
}
private function createContent():void
{
_content = new Content();
_content.addEventListener(CustomEvent.CHANGETEXT , onChangeText)
addChild(_content);
}
public function onChangeText():void
{
_content.title = "Hello World";
_content.initialize();
}
private function createButton():void
{
_btn = new CustomButtons();
_btn.x = 100;
_btn.y = 10;
addChild(_btn)
}
}
}
Content Class:
package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
public class Content extends MovieClip
{
private var _titleField:TextField;
public var title:String;
public function Content()
{
createTitleField();
}
public function initialize():void
{
_titleField.text = title;
}
private function createTitleField():void
{
_titleField = new TextField();
_titleField.defaultTextFormat = new TextFormat("Calibri", 15);
_titleField.height = 30;
_titleField.width = 200;
_titleField.border = true;
_titleField.text = "Lorem Ipsum";
_titleField.x = 200;
_titleField.y = 5;
_titleField.mouseEnabled = false;
addChild(_titleField);
}
}
}
CustomButtons Class:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.events.MouseEvent;
public class CustomButtons extends MovieClip
{
private var _label:String;
public var _height:Number;
public var _width:Number
private var _background:Sprite=null;
private var _labelfield:TextField =null;
public function CustomButtons():void
{
createBackground();
createLabel();
this.buttonMode = true;
addEventListener(MouseEvent.CLICK, onClick)
}
private function onClick(e:Event):void
{
dispatchEvent(new CustomEvent(CustomEvent.CHANGETEXT));
}
private function createBackground():void //create background for the button
{
_background = new Sprite();
_background.graphics.beginFill(0xFFFFFF)
_background.graphics.lineStyle(3, 0x000000)
_background.graphics.drawRect(0, 0,50 , 20);
_background.graphics.endFill();
addChild(_background);
}
private function createLabel():void //create label for the button
{
_labelfield = new TextField();
_labelfield.autoSize = TextFieldAutoSize.LEFT;
_labelfield.defaultTextFormat = new TextFormat("Calibri", 12, 0x0000);
_labelfield.mouseEnabled = false;
_labelfield.text = "Change";
_labelfield.x = (_background.width - _labelfield.width) / 2;
_labelfield.y = (_background.height - _labelfield.height) / 2;
addChild(_labelfield);
}
}
}
CustomEvent Class:
package
{
import flash.events.Event;
public class CustomEvent extends Event
{
public static const CHANGETEXT:String = "changetext";
public function CustomEvent(type:String)
{
super(type);
}
}
}
but it does not work… I dispatch and event, and listen to it, but it is not working…
here is the source code: