PDA

View Full Version : Events in custom classes



Flufferino
December 6th, 2007, 08:51 AM
Ok I finally realize that I don't understand how the EventDispatcher works.
Or, perhaps, not fully understands it anyway.

My problem:
I've created a custom class, lets call it Bounce. Everytime the ball hits the ground it broadcast the event Event.CHANGE as so:


disp.dispatchEvent(new Event(Event.CHANGE));
Where disp is a EventDispatcher created when the class Bounce first is created.

Everything works great if I have once Bounce class object on the main timeline, if I add another one the CHANGE for the first one stops triggering but the last one added works instead.



var bouncyBall:Bounce = new Bounce();
var bouncyBall2:Bounce = new Bounce();

bounceyBall.addEventListener(Event.CHANGE, handler); // this works now

bounceBall2.addEventListener(Event.CHANGE, handler); // after this line one bounceBall2 responds to the Event.CHANGE
Any clues what misstake I'm doing?

Charleh
December 6th, 2007, 09:20 AM
You are using the same handler for each object right and then using event.target to modify the object?

Post your handler code

Flufferino
December 6th, 2007, 09:47 AM
You are using the same handler for each object right and then using event.target to modify the object?

Post your handler code

That's one other thing, if I trace out the target of the event in the handler it gives me:

[object EventDispatcher]where I would expect something like a Bounce Object.

The code of the handler isn't important at this moment as it only trace back stuff at the moment.

mprzybylski
December 6th, 2007, 09:49 AM
you also dont need to create the disp variable, just have Bounce extend EventDispatcher or if it is a display object have it extend whatever display object you want and it automatically can dispatch events.

also, when you create the variables, those names are different than when you call addEventListener. you have bouncyBall and then refer to bounceyBall and you have bouncyBall2 and refer to bounceBall2.

Flufferino
December 6th, 2007, 09:57 AM
also, when you create the variables, those names are different than when you call addEventListener. you have bouncyBall and then refer to bounceyBall and you have bouncyBall2 and refer to bounceBall2.

that was just a typo in the forumpost but thanks for pointing even that out. I'll try to remove the use of the disp EventDispatcher since my class in extending a DO

mprzybylski
December 6th, 2007, 10:01 AM
ok, in that case we actually do need to see your handler then to see how you are handling the CHANGE event.

Flufferino
December 6th, 2007, 10:09 AM
ah dispatching straight from within the class instead of disp gives me the correct Bounce object back when tracing target. And that in the end solved my original problem with eventListeners writing over each other. Many thanks mprzybylski

mprzybylski
December 6th, 2007, 10:13 AM
glad i could help.

Dazzer
December 6th, 2007, 10:28 AM
An educated guess on the problem.

you used a static var for the EventDispatcher. Thus when the 2nd one was created, it overwrote the first one.

Just my guess.

But glad it is fixed anyhow.