views:

1430

answers:

3

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?

+2  A: 

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;

JStriedl
Hey that code is correct but actually my function and eventListener are inside of the Navigation Class, and the event function contains code specific to the Navigation Class, so how would I add the eventListener to the Class of which its a part off? I thought this. would have worked
Leon
I'm trying to get this code to work actually, reversing my thinking... where/when would the attachListenerForIntro function be called?
Leon
I'm a little unclear on how your app is organized. Can you edit your question to include the full code from the classes involved?
JStriedl
Ok I added more of my code for the CustomEvent Class and Intro Class, there is too much in the Navigation Class. Basically again I have a button made in the Intro class that I need to call a function inside of the Navigation Class. Intro and Navigation are both sub classes(not my Main class)
Leon
edited the answer to show code example. P.S. 'subclass' typically means something different than 'not the document class' so using that term improperly can lead to confusion (as it did for me here) ;)
JStriedl
I'm not getting any errors, well cept for var navigator:Navigator;Not sure what that is suppose to reference? nv is the var I use in my Main class to reference Navigation. without navigator I'm still not getting the function to get called however :(
Leon
The name 'navigator' in the example was chosen to be illustrative, as I haven't seen your code I can't give you a copy-paste version. You're going to need to translate this to fit your needs. Change 'navigator' to 'nv' if that fits in your code. The reference to 'navigator' or 'nv' is sort of the whole point, if it's excluded it won't work. The important question here is: Do you understand the concept that I'm trying to communicate?
JStriedl
Hi JStriedi I'm trying to make this work using your code again per Joel's comment. How would I call the attachListenerForIntro(); function in the Navigation class? I setup a dispatch event now form the Intro class, but am unsure of how to trigger that function. Also attachListenerForIntro( is expecting something here ) not sure what...
Leon
I think I will post this as a new Question (part 2) with updated code(mine and yours), hopefully you could take a 2nd look at it :)
Leon
Here we go :) http://stackoverflow.com/questions/1759567/flash-as3-calling-a-function-in-a-class-from-another-class-part-2-via-dispatch
Leon
A: 

I used public static var so I could reference the Class I was trying to call a function in directly, instead of using event dispatchers.

First part: Navigation class

public class Navigation extends MovieClip
{
 private var // where I put many private vars
 private var intro:Intro;
 public var // where I put many public vars
 public static var instance:Navigation;


 // ☼ --------------------------------- Constructor
 public function Navigation():void
 {
  instance = this; //<- so I can reference this Class from another
        //other code

Intro class button action:

private function clicked(e:MouseEvent):void 
 {
  trace("clicked play -> launch function in Navigation");
  Navigation.instance.introPlayButtonClick(); //Class targeted
 }

Now I can from inside the Intro class, call this function in my Navigation class:

public function introPlayButtonClick():void
 {    
  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;
 }
Leon
I think your solution is horrible. If you are going to use this Singleton pattern then you need to guard against multiple instances of Navigation from occurring, otherwise you'll get horrid ambiguity. Certainly it **is** a solution, but the other answer is the proper approach in terms of clean objected oriented development.
Joel Hooks
Hey Joel thx for the explanation, I'm still having trouble with JStriedl's code, I'm going to post a part 2 of this question... hopefully get a fresh look it, I want to do it the right way.
Leon
Here it is :) http://stackoverflow.com/questions/1759567/flash-as3-calling-a-function-in-a-class-from-another-class-part-2-via-dispatch
Leon
A: 

Thank you so much... It really save my time! Excellent!!

Bruno Amorim
You're welcome :)
Leon