Flash-AS3:通过 DispatchEvent 从另一个类调用一个类中的函数(第 2 部分)

发布于 2024-08-11 05:53:46 字数 3346 浏览 9 评论 0原文

嘿,这个问题是对 Joel Hooks 对我的一个旧问题的评论的回复(如何从另一个类调用一个类中的函数?

我能够使用公共静态变量来引用我的一个类来解决我自己的问题,因此在另一个类中我只需要使用 this 关键字以便可以调用它。

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

现在看来我并没有以最好的或纯粹的方式为OOP(可以在这里感受到争论)做这件事,所以我希望第二次提出这个问题正确地弄清楚这一点。

问题

我有 2 个课程,简介和导航。 Intro 类中创建了一个按钮,需要调用导航类内部的函数。我还有一个 CustomEvent 类,我可以在其中设置自定义事件。下面是代码,我删除了与此问题相关的所有不必要的代码。

Intro 类

当调用函数 clicked 时,它会调度 Navigation 类正在侦听的事件。


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

导航类

这是导航类,其中包含介绍类需要能够调用的函数。


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

问题或错误

到目前为止,单击按钮时没有任何反应:( 我假设我在导航类中没有正确设置 EventListener?

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

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.

The Problem

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.

The Intro Class

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋千易 2024-08-18 05:53:46

通常,容器有责任调用其子级的函数。

如果您想让(否则独立的)兄弟姐妹彼此交互,您的容器将充当两者之间的代理。

例如,如果您的应用程序的结构如下:

container
  |
  +-- introInstance
  |
  +-- navigationInstance
  1. containerintroInstance 上添加 CustomEvent.INTRO_CLICKED 事件的侦听器。< /p>

  2. introInstance 引发 CustomEvent.INTRO_CLICKED 事件。

  3. container 的事件处理程序中调用 navigationInstance.introPlayButtonClick()

通过这种方式,您的 Intro 类不需要了解有关您的 Navigation 类的任何信息,反之亦然,您的 container 形成了两者之间的粘合剂两个实例。

快速说明:容器 不必是一个在其显示列表中包含两个项目的文字 DisplayObjectContainer,它只需拥有对两个实例的引用并知道如何处理它们即可。

Generally it's the responsibility of the container to call functions on its children.

If you want to have (otherwise independent) siblings interacting with each other your container acts as a proxy between the two.

For example, if your application is structured like this:

container
  |
  +-- introInstance
  |
  +-- navigationInstance
  1. container adds a listener for the the CustomEvent.INTRO_CLICKED event on introInstance.

  2. introInstance raises the CustomEvent.INTRO_CLICKED event.

  3. In container's event handler call navigationInstance.introPlayButtonClick().

In this way your Intro class doesn't need to know any thing about your Navigation class and vice-versa, your container forms the glue between the two instances.

Quick note: The container doesn't have to be a literal DisplayObjectContainer with both items on its display list, it just has to have a reference to both instances and know what to do with them.

傾城如夢未必闌珊 2024-08-18 05:53:46

呃..我能想到的唯一方法是将您的 Intro 类扩展到 Navigation 类,以便使其正常工作。

类似的东西..

public class Intro extends Navigation {}

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

Something like..

public class Intro extends Navigation {}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文