PDA

View Full Version : Custom Events question



xakepa_2004
May 31st, 2009, 04:35 AM
Ok, I've searched quite a lot but can't find a fix to my problem. I'm doing quite a complicated game with a lot of code but one thing I can't quite figure out is how to trigger a function on the main stage from a dynamically added object.I've tried using custom events, but it doesn't work and doesn't throw any errors either.

Here's the code for the CustomEvent class:


package
{
import flash.events.Event;

public class CustomEvent extends flash.events.Event
{
public static const CUSTOM:String = "CustomEvent";
private var command:String;
var custom:*;

public function CustomEvent(object:Object)
{
super(CUSTOM);
this.custom = object;
}
}
}


Here's the code part in the dynamically added child(CustomEvent is imported):


this.parent.dispatchEvent(new CustomEvent(null));


and here's the main timeline part of the code:



stage.addEventListener(CustomEvent.CUSTOM, checkMoney);
//more code here
function checkMoney(e:CustomEvent)
{
trace("money");
}


All of the above seems to work, because it doesn't throw an error, but it doesn't trace money when the event is dispatched.Before you ask, the event IS being dispatched or at least the if method it's in is entered.Any other method to tell the stage it's time to run the function is welcome also.Thanks all in advance.

P.S. about referencing I've tried addEventListener, this.addE... and stage.addE... for the main timeline and dispatchEvent,this.disp... and this.parent.disp... for the class. No combination of these works...

m90
May 31st, 2009, 05:35 AM
I'm not a 100% sure but I guess it has got something to do with the fact that you pass a "null" to your CustomEvent class? Why do you do this?

And why do you use custom events at all? A simple:


dispatchEvent(new Event('HELLO'));
//together with
stage.addEventListener('HELLO',sayHello);


should work the same way? Or am I missing something here?

xakepa_2004
May 31st, 2009, 05:53 AM
Last edit: YAY, it worked, thanks A LOT for the hint, never would've guessed. It's a very nifty trick! Thanks again!

m90
May 31st, 2009, 06:10 AM
I think you shouldn't be listening to the stage dispatching the event but your custom Object.

Like if you have this Class:


package{

import flash.events.Event;
import flash.display.Sprite;

public class Dummy extends Sprite{

public function Dummy():void{
}

public function dispatchDummyEvent():void{
dispatchEvent(new Event('DUMMY'));
}


}

}


You would listen for the event like this:


import Dummy;

var dummy:Dummy = new Dummy();
stage.addChild(dummy);
dummy.addEventListener('DUMMY',sayHello);
dummy.dispatchDummyEvent();

function sayHello(e:Event):void{
trace('yay');
}

m90
May 31st, 2009, 06:20 AM
Ok, sorry for missing this before, also you can tell the Event to "bubble" by creating it like:


dispatchEvent(new Event('MONEY',true,false));
which will enable you to add the EventListener to the stage as the Event now will bubble up. Didn't know this was turned off by default?

xakepa_2004
May 31st, 2009, 07:32 AM
Well what I finally did was in the class added this.parent.dispatchEvent,which worked OK and I'm sure that the parent will always be the main Timeline.I'm done for today but I'll try bubbling it tomorrow and see which one's better. Thanks again for your help!

P.S. Adding listeners to each custom object is worse in my situation, cause there may be more than 50 instances on stage at one time.And the way I did it there's only one listener no matter what.

Felixz
June 3rd, 2009, 04:09 AM
Don't forget to override the clone() (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/events/Event.html#clone())