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

大家好:)

我今天的问题围绕着我试图从一个子类发送到另一个子类的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函数中没有任何痕迹。

我是如何通过变量名称nv将eventListener附加到Navigation类的,它在我的主类中通过PAUSE_MOVIE事件如何工作:
nv.addEventListener("onPause", pauseMovie); // Inside of MAIN class

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

有什么想法,技巧,想法吗?

最佳答案

侦听器附加到调度任何给定事件的类的实例上,因此要在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,该指向对象指向NAVIGATION OBJECT中的事件处理函数。因此,如果您在主类中同时引用了这两种方法,请执行以下操作:
var navigator:Navigator;
var intro:Intro;

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

因此,在Navigation类中,您需要引用Intro类来附加事件侦听器,这是attachListenerForIntro函数的关键所在。因此,在您的主类中,我假设您对Intro以及Navigation实例都有引用。如果在主类中您有类似var nv:Navigation的内容; var intr:Intro;

关于flash - 如何从另一个类调用一个类中的函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1638537/

10-08 21:35