如何调用从另一个类的类的功能?(How to call a function in a Class

2019-09-02 01:09发布

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

大家好 :)

我今天的问题围绕着一个自定义事件我想送从一个子类到另一个。

我用我的自定义事件类从子类的事件传递给我的主类很好,但我不知道是谁做的子类之间。

我的自定义事件类

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

}

子类导航-在我听该事件需要调用导航类的内部此功能:

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功能。

它在我的主类的工作原理与PAUSE_MOVIE事件,是我通过变量名NV附事件监听到导航类:

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

但自从我加入了事件侦听器的导航类里面的INTRO_CLICKED事件我以为所有我需要的是(这)增加,但到目前为止,我没有收到该事件触发。

任何想法,建议,想法?

Answer 1:

侦听器附连到调度任何给定的事件的类的一个实例,因此以监听来自导航类的实例中的介绍对象的情况下,则需要“的addEventListener”到该介绍对象,这意味着需要一个参考在介绍类的实例附加到。

不知道有足够的了解您的设置给一个更详细的答案,这里的总体思路的一个说明性小例子:

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

编辑:基于注释更新例如:

所以,你需要附加事件监听器指向导航器对象的事件处理函数的介绍对象。 因此,如果你在你的主类有这两个基准,做这样的事情:

var navigator:Navigator;
var intro:Intro;

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

因此,从航海类中,你需要引用前奏类附加事件监听器,这是attachListenerForIntro功能的点。 所以从你的主类,我认为你必须前奏参考以及导航实例。 如果在主类中,你有这样的事情变种NV:导航; VAR INTR:介绍;



Answer 2:

我使用的公共静态无功 ,所以我可以参考,而不是使用事件的调度员,我试图调用一个函数直接在类。

第一部分: 导航类

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
    }

现在我可以从前奏类中,调用这个函数在我的导航类:

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


Answer 3:

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

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



文章来源: How to call a function in a Class from another Class?