• AS3 EventListener Woes
    6 replies, posted
I'm trying to catch some custom events but... it's not happening. I'm not completely up to snuff on my AS3 so please forgive me for my sins. :v: I'm trying to dispatch an event inside a class created by the main class. I know the custom Event works, it just doesn't in this case. Main.as (Snippet, there's more to this, it compiles fine) [lua] package { public class Main extends MovieClip { public function Engine() { var beatManager = new BeatManager(); addChild(beatManager); addEventListener( PlayerEvent.BEAT, onBeatDetected ); } private function onBeatDetected(e:BeatEvent):void { trace("Beat!") } } } [/lua] [b]"Beat!" Never gets called.[/b] Inside BeatManager.as [lua] private function onBeatDetected(e:BeatEvent):void { if (e.magnitude > _beatThreshold) { trace("Beat detected") _specBeatCirlce.update(e.magnitude); var event:PlayerEvent = new PlayerEvent(PlayerEvent.BEAT); dispatchEvent( event ); //dispatchEvent( new PlayerEvent(PlayerEvent.BEAT) ); } ++_beatCount; } [/lua] [b]NOTE: THIS IS GETTING CALLED. IT DOES PRINT "Beat detected" TO THE OUTPUT![/b] PlayerEvent.as [lua] package { import flash.events.Event; /** * ... * @author Matt */ public class PlayerEvent extends Event { public static const DEAD:String = "dead"; //TODO: Move these two a navigation/wave event instead of player event? public static const FINISHEDWAVE:String = "finishedwave"; public static const SENDWAVE:String = "sendwave"; public static const BEAT:String = "beat"; public function PlayerEvent( type:String ) { super( type ); } } }[/lua] So I know the function is getting called inside BeatManager.as, and I assume it's actually dispatching the PlayerEvent. However, after that I lose it and don't know if it actually dispatches or if my listener is wrong? Any ideas? Also: When I run the code in debug, BeatManager.as throws: SecurityError: Error #2121: Security sandbox violation: SoundMixer.computeSpectrum: file:///C|/Users/Matt/Documents/Flash/Test.swf cannot access. It only does this when trying to debug however. [b]Edit:[/b] Apparently this is an issue with SoundMixer.computeSpectrum...
I'm kind of doubting you understand what you are doing. It's hard to understand anything when you're just throwing a bunch of incomplete snippets.
Dunno what else you want, I posted all of the relevant snippets... I'm creating an event listener in my main application, and am dispatching a custom event in a created class. ( var beatManager = new BeatManager(); addChild(beatManager); addEventListener( PlayerEvent.BEAT, onBeatDetected ); ) I can post other working examples of PlayerEvent if you'd like.
Maybe I am completely missing something, so I'm going to ask you to clarify. [code]private function onBeatDetected(e:BeatEvent):void[/code]Why do you cast 'e' as a BeatEvent? Shouldn't it be a PlayerEvent? It's the only place I see a reference to BeatEvent. My guess is that the PlayerEvent class used to be called BeatEvent and you forgot to change it.
It's not casting, "e:BeatEvent" in AS is the same as "BeatEvent e" in C/C++/C# If that was a problem the compiler would complain about passing PlayerEvent to the function that expects a BeatEvent.
Yes, but if for some reason he has a BeatEvent class imported then it won't throw an error and based off the code it should be casted the way I said. I don't see where this BeatEvent class is coming from and it doesn't make sense to use it in that function because PlayerEvent extends event. It doesn't make sense to me why it is being used, but maybe there is a reason I'm not aware of. Also, is Engine even being called? Since the OP is leaving out what he doesn't think is important I can't say for sure, but judging from the fact that the function is capitalized and has no return value while other functions do, I think that he forgot to rename the Engine class to Main. Could I be wrong, of course, but I can only base my assumption on what he is giving.
Whoops. Okay, I totally get where you guys are coming from and how it's just snippets. I apologize for that. I figured it out finally. Event Listeners can't listen for events that are "down" stream from them in a parent/child relationship. Because of that, I had to add an event listener to the specific object in the engine, via a getter/setter function. It works now, thanks all.
Sorry, you need to Log In to post a reply to this thread.