Splyth
Splyth
  • Member since: Sep. 13, 2008
  • Offline.
Forum Stats
Member
Level 07
Programmer
Custom Event Woes Aug. 1st, 2012 @ 08:06 PM Reply

Hi all. Now I did my homework about making Custom events in as3 and to date I haven't been able to get the silly thing to work. I've checked it out and it does indeed dispatch my custom event but my listener never receives it.

here's what going on

My Custom Event:

import flash.events.Event
	public class BulletEvent extends Event
	{
		public static const BULLET_EVENT:String = "bulletEvent";
		public var dataObj:String;
		public function BulletEvent(type:String, dataObj:String = null) 
		{
			this.dataObj = dataObj;
			super(type, false, false);
		}
		
		override public function clone():Event
		{
			return new BulletEvent(type, dataObj);
		}
	}

my dispatcher dude he does work and he's located in my hero class

if (spaceKey)
			{
				var data:String = "HeroShot"
				dispatchEvent(new BulletEvent(BulletEvent.BULLET_EVENT, data));
			}

Now my eventlistener guy. He's the dude that isn't catching my event

package Bullets 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class BulletManager extends Sprite
	{
		
		public function BulletManager() 
		{
			addEventListener(BulletEvent.BULLET_EVENT, handleBullet);
		}
		
		public function handleBullet(event:BulletEvent):void 
		{
			var mydata:String = event.dataObj;
			trace("works");
		}
		
	}

}

Thanks in advance

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 15
Programmer
Response to Custom Event Woes Aug. 1st, 2012 @ 08:27 PM Reply

You should probably get rid of the dataObj var and instead add more types of BulletEvents. Like BulletEvent.HeroShot or something. Either way, I assume you put a trace after the dispatchEvent function, but try putting one where you add the listener. I do the same stuff with my events, minus the dataObj, and it's fine/

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Supporter
Level 10
Programmer
Response to Custom Event Woes Aug. 1st, 2012 @ 08:39 PM Reply

Since you attached the listener to the BulletManager, that object must dispatch the event, if any objects inside the object dispatch it that listener will not catch it. If you want nested objects to be caught by that listener you'll need to bubble it.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
Splyth
Splyth
  • Member since: Sep. 13, 2008
  • Offline.
Forum Stats
Member
Level 07
Programmer
Response to Custom Event Woes Aug. 1st, 2012 @ 09:06 PM Reply

At 8/1/12 08:39 PM, MintPaw wrote: Since you attached the listener to the BulletManager, that object must dispatch the event, if any objects inside the object dispatch it that listener will not catch it. If you want nested objects to be caught by that listener you'll need to bubble it.

That's probably my issue. I was trying to catch an event outside the class that dispatched it.
thanks; oh and nice sig.