[FONT=Arial][SIZE=2]I’ve embarked on my first as3 project and I’m having trouble passing parameters through event listeners.
I’ve spent most of the day searching forums and trying things out but haven’t been able to get it to work. I was wondering if anyone could point me in the right direction.
I understand I need to either build or extend a class to be able to do this (I’m a newbie with classes BTW).
Also, just to mention, my code is all in the flash movie the main timeline in the first frame, I didn’t use a document class (Haven’t got the knowledge yet).[/SIZE][/FONT]
[FONT=Arial][SIZE=2]
The project involves an interface with multiple windows that are ‘draggable’ and ‘closeable’.[/SIZE][/FONT]
[FONT=Arial][SIZE=2]This is what I’ve got so far, I’ve reduced the code to just the bare essentials to make things easier to read.[/SIZE][/FONT]
[FONT=Arial][SIZE=2]I was using anonymous functions before on mouseEvents (ex addEventListener(MouseEvent.CLICK, function(evt:Event) { etc. etc. } ),
but I’d like to be able to remove my listeners, since I have quite a few listeners running for all the button clicks, drags, mouseOvers, MouseOuts etc. and I don’t want memory leaks.[/SIZE][/FONT]
[FONT=Arial][SIZE=2]To illustrate my project on a small scale, let’s say I’ve got 3 windows and 3 buttons[/SIZE][/FONT]
[FONT=Arial][SIZE=2]MC_windowHome, MC_windowAbout, MC_windowServices[/SIZE][/FONT]
[FONT=Arial][SIZE=2]BTN_Home, BTN_About, BTN_Services[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Each window contains a background and a close button named ‘windowBG’ and ‘BTN_close’[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Here is the code I have (abridged of course) and the event listeners are only exemplary and not functional[/SIZE][/FONT]
// Functions
Function OpenWindow(windowName:String) {
var window:MovieClip = MovieClip(this.getChildByName(windowName));
window.visible = true;
//This is where I need a custom class to insert the window name parameter ideally keeping the capturePhase, priority, and useWeakReference window. windowBG.addEventListener(MouseEvent.MOUSE_DOWN, DragWindow, false, 0, true, PARAMETERS [windowName]);
window. windowBG.addEventListener(MouseEvent.MOUSE_UP, StopDragWindow, false, 0, true, PARAMETERS [windowName]);
window. BTN_close.addEventListener(MouseEvent.CLICK, CloseWindow, false, 0, true, PARAMETERS [windowName]);
}
Function CloseWindow(windowName:String) {
var window:MovieClip = MovieClip(this.getChildByName(windowName));
window.visible = false;
//This is where I need a custom class to remove the event listeners for the closed window
window. windowBG.removeEventListener(MouseEvent.MOUSE_DOWN, DragWindow, false, 0, true, PARAMETERS [windowName]);
window. windowBG.removeEventListener(MouseEvent.MOUSE_UP, StopDragWindow, false, 0, true, PARAMETERS [windowName]);
window. BTN_close.removeEventListener(MouseEvent.CLICK, CloseWindow, false, 0, true, PARAMETERS [windowName]);
}
Function DragWindow(windowName:String){
var window:MovieClip = MovieClip(this.getChildByName(windowName));
window. startDrag();
}
Function StopDragWindow(windowName:String){
var window:MovieClip = MovieClip(this.getChildByName(windowName));
window.stopDrag();
}
// Buttons
BTN_Home.addEventListener(MouseEvent.CLICK, OpenWindow, false, 0, true, PARAMETERS [windowName]);
BTN_About.addEventListener(MouseEvent.CLICK, OpenWindow, false, 0, true, PARAMETERS [windowName]);
BTN_Services.addEventListener(MouseEvent.CLICK, OpenWindow, false, 0, true, PARAMETERS [windowName]);
[FONT=Arial][SIZE=2]I tried using this CLASS but I couldn’t get it to work properly with the following call
dispatchEvent(new CustomEvent(CustomEvent. mouseclick, ‘MC_WindowHome’)) ;[/SIZE][/FONT]
[FONT=Arial][SIZE=2]BTN_home.addEventListener(CustomEvent.mouseclick, OpenWindow, false , 0, true);[/SIZE][/FONT]
package {
import flash.events.Event;
import flash.events.MouseEvent;
//custom event class to enable the passing of strings along with the actual event
public class CustomEvent extends Event
{
public static const mouseclick :String = "click";
public static const mousedown :String = "mouseDown";
public static const mouseup :String = "mouseUp";
public var parameterText:String;
public function CustomEvent (type:String, searchText:String)
{
this.parameterText = searchText;
super(type);
}
}
[FONT=Arial][SIZE=2]}[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Would anyone be able to point me in the right direction or suggest a better alternative to what I’d like to accomplish? I apologize for my inability to figure this one out.[/SIZE][/FONT]