Update: Modified title to better reflect my question
Hi everybody :)
My question today revolves around a CustomEvent I'm trying to send from one sub Class to another.
I've used my CustomEvent class to pass an event from a sub Class to my main class fine, but I'm not sure who to do that between sub classes.
My Custom Event Class
package src.events {
import flash.events.Event;
public class CustomEvent extends Event
{
public static const CHANGE:String="onChange";
public static const COMPLETE:String="onComplete";
public static const IMAGE_LOADED:String="onImageLoaded";
public static const SWF_LOADED:String="onSWFLoaded";
public static const SWF_UNLOAD:String="onSWFUnload";
public static const CLICK:String="onClick";
public static const PAUSE_MOVIE:String="onPause";
public static const INTRO_CLICKED:String="introClicked";
public var params:Object;
public function CustomEvent(type:String, params:Object, bubbles:Boolean = false, cancelable:Boolean = false)
{
super(type, bubbles, cancelable);
this.params=params;
}
public override function clone():Event
{
return new CustomEvent(type, this.params, bubbles, cancelable);
}
public override function toString():String
{
return formatToString("CustomEvent","params","type","bubbles","cancelable");
}
}
Sub Class INTRO - dispatches event
package src.display {
import gs.easing.*;
import gs.TweenLite;
import flash.text.*;
import flash.events.*;
import flash.display.*;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
// ☼ ----------------------------------------- Imported Custom Classes
import src.events.CustomEvent;
// ☼ ----------------------------------------- Intro Class
public class Intro extends Sprite
{
private var playBtn:MovieClip;
// ☼ ----------------------------------------- Constructor
public function Intro():void
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
// ☼ ----------------------------------------- Init
public function init(event:Event):void
{
draw();
}
public function draw():void
{
playBtn = new PlayThumb;
playBtn.buttonMode = true;
playBtn.x = 582;
playBtn.y = 259;
playBtn.alpha = 1;
addChild(playBtn);
playBtn.addEventListener(MouseEvent.MOUSE_UP, clicked);
}
private function clicked(e:MouseEvent):void
{
trace("clicked big play");
dispatchEvent (new CustomEvent(CustomEvent.INTRO_CLICKED, {}));
trace("event = "+CustomEvent.INTRO_CLICKED);
}
}
Sub Class NAVIGATION - where I'm listening for the event Need to call this function inside of the Navigation class:
function introPlayButtonClick(e:CustomEvent):void
{
trace("introPlayButtonClick called");
TweenLite.to(buttons[0], 1, {y:20, ease:Strong.easeOut});
TweenLite.to(buttons[1], 1, {y:navBtnY, ease:Strong.easeOut});
TweenLite.to(introTab, 1, {y:60, ease:Strong.easeOut});
TweenLite.to(scrollerMov, 1, {y:60, ease:Strong.easeOut});
$btn1 = true;
$btn2 = false;
trace("introPlayButtonClick called --- end");
}
Ok so I believe my problem lies in the Navigation Class, how do I correctly add the addEventListener here? I'm not getting any of the traces in my introPlayButtonClick function.
How it worked in my Main class with the PAUSE_MOVIE event, was I attached the eventListener to the Navigation class via the variable name nv:
nv.addEventListener("onPause", pauseMovie); // Inside of MAIN class
But since I'm adding the event listener inside of the Navigation class for the INTRO_CLICKED event I thought all I needed was (this.) added, but so far I'm not getting that event to trigger.
Any ideas, tips, thoughts?
listeners are attached to an instance of the class that dispatches any given event, so to listen for the event from an Intro object within an instance of the Navigation class, you need to 'addEventListener' to that Intro object, which means you need a reference to an instance of the Intro class to attach to.
Not knowing enough about your setup to give an more detailed answer, here's an illustrative little example of the general idea:
public class Navigation extends WhateverClassYourSubClassing
{
public var intro:Intro;
public function attachListenerForIntro(introToCheckForClick:Intro){
intro = introToCheckForClick;
intro.addEventListener("onIntro", introPlayButtonClick);
}
public function introPlayButtonClick(e:CustomEvent):void
{
trace("introPlayButtonClick called");
TweenLite.to(buttons[0], 1, {y:20, ease:Strong.easeOut});
TweenLite.to(buttons[1], 1, {y:navBtnY, ease:Strong.easeOut});
TweenLite.to(tabNum1, 1, {y:60, ease:Strong.easeOut});
TweenLite.to(scrollerMov, 1, {y:60, ease:Strong.easeOut});
$btn1 = true;
$btn2 = false;
trace("introPlayButtonClick called --- end");
intro.removeEventListener("onIntro", introPlayButtonClick);
}
}
EDIT: updated example based on comments:
So you need to attach the event listener TO THE INTRO OBJECT that points to the event handler function IN THE NAVIGATION OBJECT. so if you have a reference to both of these in your main class, do something like this:
var navigator:Navigator;
var intro:Intro;
intro.addEventListener("onIntro", navigator.introPlayButtonClick);
So from within the Navigation class, you need to reference the Intro class to attach the event listener, which is the point of the attachListenerForIntro function. So from your main class, I assume you have a reference to the Intro as well as the Navigation instances. if in the main class you had something like var nv:Navigation; var intr:Intro;