本文介绍了Flash的AS3:从另一个类中调用的类函数(第2部分)通过则dispatchEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,这问题在回复乔尔·胡克斯的评论对我的一个老问题(How从另一个类调用一个函数的类?)

Hey this question is in reply to Joel Hooks's comment on an older question of mine (How to call a function in a Class from another Class?)

我是能够使用公共静止无功参考我的职业之一,解决我自己的问题,所以在其他类我只需要使用这个关键字,因此它可以被调用。

I was able to fix my own problem using a public static var to reference one of my Classes, so in the other class I just needed to use the this keyword so it could be called.

instance = this; // in Navigation Class
Navigation.instance.introPlayButtonClick(); // in Intro Class

现在看来,我不这样做的最好的还是纯粹的办法 OOP (可以在这里感受到的辩论),所以我冒充这个问题第2时间得到这个的希望想出正确。

Now it seems I'm not doing this in the best or purist way for OOP(can sense debate here), so I'm posing this question a 2nd time in hopes of getting this figured out correctly.

我有2个班,介绍和导航。有在需要调用一个函数导航类的内部的介绍类创建的按钮。我也有一个自定义事件类,我设置自定义事件。下面是code,我已经去掉了所有有关这个问题不重要code。

I have 2 Classes, Intro and Navigation. There is a button created in the Intro class that needs call a function inside of the Navigation Class. I also have a CustomEvent class where I setup Custom Events. Below is the code, I've stripped out all of the unessential code relating to this problem.

在该功能的点击被调用,将分派一个事件,导航类监听。

When the function clicked is called, it dispatches an Event that the Navigation class is listening for.


package src.display
{
import flash.events.*;
 import flash.display.*;

 // ☼ --- Imported Classes
 import src.events.CustomEvent;
 import src.model.Navigation;

 public class Intro extends Sprite 
 {
  private var playBtn:MovieClip;
  private var colorTransform:ColorTransform;

  // ☼ --- Constructor
  public function Intro():void 
  {
      this.addEventListener(Event.ADDED_TO_STAGE, init);
  }

  // ☼ --- Init
  public function init(event:Event):void 
  {
      draw();
      this.removeEventListener(Event.ADDED_TO_STAGE, init);
  }

  public function draw():void 
  {
      playBtn = new PlayThumb;
      playBtn.buttonMode = true;   
      playBtn.x = 552;   
      playBtn.y = 289;
      playBtn.alpha = 1;

      // ☼ --- add button 
      addChild(playBtn);

      // ☼ --- button listeners
      playBtn.addEventListener(MouseEvent.MOUSE_UP, clicked);
  }
  //-----------------------------------------------------
  // ☼ --- My Old Function
  /*private function clicked(e:MouseEvent):void 
  {
      Navigation.instance.introPlayButtonClick(); // Open tab1
  }*/
  //-----------------------------------------------------
  // ♦♦♦ Added to dispatch Event to JStriedl's function ♦♦♦
  private function clicked(e:MouseEvent):void 
      {
      dispatchEvent (new CustomEvent(CustomEvent.INTRO_CLICKED, {}));
      trace("Dispatch Event INTRO_CLICKED");
      }
 }
}

导航类

这是导航类,它包含了介绍类需要能够调用一个函数。

Navigation Class

This is the Navigation Class which contains a function that the Intro Class needs to be able to call.


package src.model 
{
import flash.events.*;
import flash.display.*;

// ☼ ---Imported  Classes
import src.display.Intro;
import src.model.Fonts;
import src.events.CustomEvent;

public class Navigation extends MovieClip //Needs MovieClip
{  
    public var intro:Intro;
    public static var instance:Navigation;

    // ☼ --- Constructor
public function Navigation()
{
    this.addEventListener(Event.ADDED_TO_STAGE, init);
}

// ☼ --- Init
public function init():void
{
    instance = this;  
    intro = new Intro;
    attachListenerForIntro(intro);
    this.removeEventListener(Event.ADDED_TO_STAGE, init);
}

// ♦♦♦ JStriedl's code ♦♦♦
public function attachListenerForIntro(introToCheckForClick:Intro)
{
    intro = introToCheckForClick;
    intro.addEventListener("introClicked", introPlayButtonClick);
}

// Function that button in Intro needs to call
public function introPlayButtonClick(e:CustomEvent):void
{    
    trace("Navigation function called from Intro");
    intro.removeEventListener("introClicked", introPlayButtonClick);
}
}
}

的问题或错误

所以,按钮被点击没事的时候为止发生:(我假设我没有事件监听器设置导航类里面是否正确?

Issues or Errors

So far when the button is clicked nothing happens :( I'm assuming I don't have the EventListener setup correctly inside of the Navigation Class?

推荐答案

Errr ..我能想到的是扩大你的介绍类导航类,以便使其正常工作的唯一方法。

Errr.. the only way I could think is extending your Intro class to the Navigation class in order for this to work properly.

喜欢的东西..

public class Intro extends Navigation {}

这篇关于Flash的AS3:从另一个类中调用的类函数(第2部分)通过则dispatchEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 11:14