2

更新:修改标题以更好地反映我的问题

大家好你们好 :)

我今天的问题围绕着我试图从一个子类发送到另一个子类的 CustomEvent。

我已经使用我的 CustomEvent 类将事件从子类传递到我的主类,但我不确定在子类之间由谁来做。

我的自定义事件类

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");
    }

}

子类 INTRO - 调度事件

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);
    }

}

子类 NAVIGATION - 我正在侦听事件 需要在 Navigation 类中调用此函数:

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");
        }

好的,所以我相信我的问题在于导航类,如何在此处正确添加 addEventListener?我的 introPlayButtonClick 函数中没有任何痕迹。

它是如何在我的 Main 类中使用 PAUSE_MOVIE 事件工作的,我是通过变量名 nv 将 eventListener 附加到 Navigation 类:

nv.addEventListener("onPause", pauseMovie); // Inside of MAIN class

但是由于我在导航类中为 INTRO_CLICKED 事件添加了事件侦听器,所以我认为我需要的只是(this.)添加,但到目前为止我还没有触发该事件。

任何想法,提示,想法?

4

3 回答 3

3

侦听器附加到调度任何给定事件的类的实例,因此要侦听来自 Navigation 类实例中的 Intro 对象的事件,您需要将“addEventListener”添加到该 Intro 对象,这意味着您需要一个引用到要附加到的 Intro 类的实例。

对您的设置不够了解,无法给出更详细的答案,这里有一个关于一般想法的说明性小例子:

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);
    }
}

编辑:根据评论更新示例:

因此,您需要将事件侦听器附加到指向导航对象中的事件处理函数的 INTRO OBJECT。因此,如果您在主类中引用了这两个,请执行以下操作:

var navigator:Navigator;
var intro:Intro;

intro.addEventListener("onIntro", navigator.introPlayButtonClick);

因此,在 Navigation 类中,您需要引用 Intro 类来附加事件侦听器,这是 attachListenerForIntro 函数的要点。因此,从您的主类中,我假设您有对 Intro 和 Navigation 实例的引用。如果在主类中你有类似 var nv:Navigation; var intr:简介;

于 2009-10-28T17:37:26.343 回答
2

我使用了public static var,所以我可以引用我试图直接调用函数的类,而不是使用事件调度程序。

第一部分: 导航类

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

介绍类按钮操作:

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

现在我可以从Intro类中,在我的Navigation类中调用这个函数:

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;
    }
于 2009-10-28T21:12:32.820 回答
1

这解决了我的问题,看看.. :),,

http://greenethumb.com/article/23/understanding-root-and-the-document-class-in-as3

于 2011-01-27T06:17:40.887 回答