PDA

View Full Version : custom event not working



sharper_1
October 18th, 2009, 10:56 AM
Greetings,

I've made a simple package that creates a custom textfield and a circle sprite.

pressing the circle sprite is supposed to update the textfield text with some sent string.

The string is being sent by using a custom event.

The problem is, for some reason the script never reaches the function that handles the custom event....why isn't the updateText event firing??

Any ideas what i'm doing wrong here?

here is the full code:

Building the objects on the first frame in fla:


var m_circleSprite:circleSprite = new circleSprite(this);
var myText:myTextField = new myTextField();
this.addChild(myText);


The circleSprite Class:


package
{
import flash.display.Sprite;
import flash.events.*;

public class circleSprite extends EventDispatcher
{
//defining a new variable that will be the circle
public var m_circleSprite:Sprite = new Sprite();
public function circleSprite(myStage)
{
this.m_circleSprite.graphics.beginFill(0x000000);
this.m_circleSprite.graphics.drawCircle(270, 200, 50);
this.m_circleSprite.graphics.endFill();
this.m_circleSprite.addEventListener("click", update_textbox);
myStage.addChild(m_circleSprite);
}
public function update_textbox(e:Event):void{
dispatchEvent(new customEvent(customEvent.UPDATETEXT, "zubi" ));
}
}
}



The myTextField Class:




package
{
import flash.text.*;
import customEvent;
import flash.events.*;

public class myTextField extends TextField {
private var myBox:TextField = new TextField();

public function myTextField()
{
//this.myStage = myStage;
this.text = "INIT";
this.border = true;
this.wordWrap = true;
this.width = 150;
this.height = 40;
this.x = 230;
this.y = 310;
this.addEventListener(customEvent.UPDATETEXT,updat eText,false,0,true);
}
public function getVal():String{
return this.myBox.text;
}
public function setVal(val:String):void{
this.myBox.text = val;
}
public function updateText(evt:customEvent):void{
setVal(evt.arg);
trace(evt);
trace(evt.arg);
}
}
}



The customEvent Class:




package
{
import flash.events.Event;

public class customEvent extends Event {

public static const UPDATETEXT:String = "updateText";

public var arg:*;

public function customEvent(type:String, customArg:*=null,
bubbles:Boolean=false,
cancelable:Boolean=false) {

super(type, bubbles, cancelable);

this.arg = customArg;

}

public override function clone():Event {
return new customEvent(type, arg, bubbles, cancelable);
}

public override function toString():String {
return formatToString("customEvent", "type", "arg",
"bubbles", "cancelable", "eventPhase");
}

}
}



Thanks in advance.

rosyTown
October 19th, 2009, 02:25 AM
Try dispatch your event to the stage.



package
{
import flash.display.Sprite;
import flash.events.*;

public class circleSprite extends EventDispatcher
{
//defining a new variable that will be the circle
public var m_circleSprite:Sprite = new Sprite();
public function circleSprite(myStage)
{
this.m_circleSprite.graphics.beginFill(0x000000);
this.m_circleSprite.graphics.drawCircle(270, 200, 50);
this.m_circleSprite.graphics.endFill();
this.m_circleSprite.addEventListener("click", update_textbox);
myStage.addChild(m_circleSprite);
}
public function update_textbox(e:Event):void{
stage.dispatchEvent(new customEvent(customEvent.UPDATETEXT, "zubi" ));
}
}
}