Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 01-19-2007, 05:34 PM   #1
Daedalus
Flexified
 
Join Date: Aug 2005
Location: Bath, UK
Posts: 127
Question dispatching a custom event from another event handler method

my custom event class
Code:
package app
{
	import flash.events.Event
	public class BoxEvent extends Event
	{
		public static const DRAWN:String = "drawn";
		
		public function BoxEvent(type:String){
			super(type);
		}
		
		public override function clone():Event{
            return new BoxEvent(type);
        }
	}
}
my class that dispatches the event

Code:
package app {
import flash.display.*;
import flash.events.*;
import flash.text.*;

public class Box extends Sprite
	{  
public function Box() {
   addEventListener(Event.ADDED_TO_STAGE, PB);
}

public function PB(ev:Event):void {
     //position the box then tell all the listeners that it is ready to be manipulated
    this.x = newX;
   this.y = newY
   this.width = stage.stageWidth;
   this.height = stage.stageHeight;
  this.dispatchEvent(new BoxEvent(BoxEvent.DRAWN));
}
  }
}
my class that creates a new box then waits until it has been positioned before interacting with it.

Code:
//BoxPlay class
headerBox = new Box();
 headerBox.addEventListener(BoxEvent.DRAWN, addContent);
this.addChild(headerBox);

public function addContent(evt:Event):void {
   //now I can play with the box
}
The code above does not work but if I dispatch the event not from my PB method (which is called when the box is added to the stage); but insted stick it in a seperate class method in my box class and call it from the BoxPlay class, it will work.

Code:
//BOX CLASS

public function rr():void {
    	 this.dispatchEvent(new BoxEvent(BoxEvent.DRAWN));
    }
Code:
//BOXPLAY CLASS

headerBox = new Box();
 headerBox.addEventListener(BoxEvent.DRAWN, addContent);
this.addChild(headerBox);
headerBox.rr();
public function addContent(evt:Event):void {
   //now I can play with the box
}
When I dispatched the event from my PB method it does not work but when dispatching from another method and calling that method from my main class it will work fine.

Another thing I tried was to keep the dispatch in the rr() method but call it from the PB() method in my box class insted of the main class, but this did not work either.

(when it does not work the event handler function simply does not get executed, there is no error);

I checked to make sure my listner was registered each time and it returned true.

Last edited by Daedalus; 01-20-2007 at 07:31 PM.
Daedalus is offline   Reply With Quote
Old 01-20-2007, 02:09 PM   #2
Tink
Addict
 
Tink's Avatar
 
Join Date: Nov 2001
Location: London
Posts: 2,128
Default

does PB get invoked? have u tried putting a trace in there to make sure?
Tink is offline   Reply With Quote
Old 01-20-2007, 04:44 PM   #3
Daedalus
Flexified
 
Join Date: Aug 2005
Location: Bath, UK
Posts: 127
Default Yes it does get invoked.

Yes it does get invoked.

I can't see anything wrong with the code but maybe it has something to do with timing as I can call the rr(); method from my BoxPlay class and it will execute the method dispatching the event which DOES cause my add addContent method to be run (so it works). But calling the rr(); method from the PB method (which is run when the box is added to the stage) cause the rr() method to run and the event to be dispatched (but it DOES NOT work) my addContent() does not get run.
Daedalus is offline   Reply With Quote
Old 01-20-2007, 05:07 PM   #4
Tink
Addict
 
Tink's Avatar
 
Join Date: Nov 2001
Location: London
Posts: 2,128
Default

can you zip the files and post them here.

Also you really should name those methods better. Its really confusing talking about 'rr' and 'PB'.
Tink is offline   Reply With Quote
Old 01-20-2007, 06:09 PM   #5
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

I dont see any huge problems, logically, at least. I noticed a missing event parameter in PB and the fact that if rr() is defined as a method of the box class, it should be called from the box instance and not as a boxplay method (which a simple call to rr() would be if from within the boxplay class).

Given that you are using ADDED_TO_STAGE, you'll also need to be sure you're running 9.0.28.0+, but if you say its being called, then thats probably not an issue.
__________________
(6)
senocular is offline   Reply With Quote
Old 01-20-2007, 07:35 PM   #6
Daedalus
Flexified
 
Join Date: Aug 2005
Location: Bath, UK
Posts: 127
Default

(errors in 1st post corrected)


ill create a new project and gradually copy over the code bit by bit and debug and test each time to find out where the problem is.
Daedalus is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
event handler scope skjc ActionScript 2.0 12 02-23-2006 11:56 AM
Database simulated with arrays on a cd-rom lecasn5 Components 61 09-07-2004 11:40 AM
Removing event handler Bjorn Johnson ActionScript 1.0 (and below) 2 03-31-2004 11:17 PM
onLoad Event Handler mundi74 ActionScript 1.0 (and below) 1 07-12-2003 08:40 PM
Adding an event handler to new component lbower ActionScript 1.0 (and below) 4 02-04-2003 08:51 PM


All times are GMT. The time now is 02:29 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.