I can’t seem to figure out how to properly use events. I have googled around a lot, read the live docs, and gone trough the “tip of day” stuff regarding events. Still can’t figure it out.
//What I have
I have a (1) Containment area, a (2) bunch of cute looking MCs and a (3) controll button.
(1), (2) and (3) are each MCs and classes. They each live in their own file.as and thus in their own package.
//What I want to do
I want to press the button, (3), have the containment class (1) register that, and then have the containment class (1) move around the pics (2).
//How I do now
What I do now is set a var in (3) that it’s been pressed, and (1) loops every 10ms to see what the status of the var is. This feels rather ugly, so I start looking for alternatives. One option, which I haven’t come to understand how to work is to call stage.(1).publicFunc() from within (2), but that seems non OOP.
//A better way?
Someone points me to the broadcaster->listener-paradigm-thingy.
I understand that the button(3) would be the broadcaster, and that the container (1) would be the listener. (3) broadcasts “hey, I was clicked”, and (1) picks that up.
Reading the examples I’ve found, I’ve made “it work” within the same class. But AFAI can understand, that’s not really the point, they’re supposed to work BETWEEN classes, right?
I seem to have some sort of out of scope problem. I can send the broadcast, but it’s not picked up.
Generally, what have I not understood?
More specifically, here’s some code that may or may not make sense, it’s what I have been experimenting with. I have no wish to be spoon-feed, but it might help you in understanding my problem. If I remove the comment // in the Document Class, I get a “didSomething” trace.
Again, I don’t really want the code below to be made working. I don’t want a fish, I wanna learn fishing
The button. (3)
package {
import flash.display.MovieClip;
import flash.events.*;
public class SomeButton extends MovieClip implements IEventDispatcher {
public function SomeButton() {
this.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(event:MouseEvent):void {
decorDispatcher.dispatchEvent(new Event("doSomething"));
}
private var isFullScreen:Boolean = false;
private var decorDispatcher:theDispatcher = new theDispatcher();
}
}
The container(1)
package {
import caurina.transitions.Tweener;
import flash.display.MovieClip;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.Timer;
public class DocumentClass extends MovieClip implements IEventDispatcher {
public function DocumentClass() {
addChild(itsmainView);
addChild(itsButton);
var decorDispatcher:theDispatcher = new theDispatcher();
decorDispatcher.addEventListener("doSomething", didSomething);
// decorDispatcher.dispatchEvent(new Event("doSomething"));
}
public function didSomething(evt:Event):void {
trace(">> didSomething");
}
private var itsButton = new SomeButton();
private var itsmainView = new mainView();
}
}
Some random code I have copy-pasted and slightly modified
package {
import flash.events.IEventDispatcher;
import flash.events.EventDispatcher;
import flash.events.Event;
class theDispatcher implements IEventDispatcher {
private var dispatcher:EventDispatcher;
public function theDispatcher() {
dispatcher = new EventDispatcher(this);
}
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
dispatcher.addEventListener(type, listener, useCapture, priority);
}
public function dispatchEvent(evt:Event):Boolean {
return dispatcher.dispatchEvent(evt);
}
public function hasEventListener(type:String):Boolean {
return dispatcher.hasEventListener(type);
}
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
dispatcher.removeEventListener(type, listener, useCapture);
}
public function willTrigger(type:String):Boolean {
return dispatcher.willTrigger(type);
}
}
}