2009-07-28 3 views
0

독립적 인 클래스 간 통신을위한 가장 간단한 방법은 무엇입니까? 2 일 동안 검색했지만 아무 것도 찾을 수 없습니다. 은 하나의 클래스에 dispatchEvent가 있고 다른 하나에는 custom Event 이벤트가있는 addEventListener가있는 방식이 아닙니까? 부모 클래스 관계가있는 동일한 클래스 o 내에서 솔루션을 찾을 수는 있지만 찾고자하는 것은 "형제"관계와 비슷합니다. 감사합니다.플래시 AS3 독립적 인 수업을 전달하는 가장 좋은 방법은 무엇입니까?

답변

0

그래서 ChildClass1, ChildClass2 및 ParentClass가있는 경우 ChildClass1과 ChildClass2는 모두 ParenClass의 자식입니다.

ChildClass1은 이벤트를 전달합니다. ParentClass는이 이벤트를 수신하고 처리기가 ChildClass2를 업데이트합니다.

0

ParentClass가없는 경우 Childs를 등록하고 그에 따라 알리는 ChildManagerClass를 사용할 수도 있습니다.

+1

예를 들어 조금 더 설명 할 수 있었습니까 ?? 감사합니다 –

+1

네, 가능하다면 설명해주십시오. 저도 관심사입니다. 감사합니다 - Katax –

2

일반적으로 이벤트를 전달하는 클래스를 EventDispatcher으로 확장하거나 IEventDispatcher으로 구현하려고합니다. (수업이 DisplayObject들 경우 모든 DisplayObject들, 그래서, 당신은 여분의 작업을 수행 할 필요가 없습니다 않습니다.)

을 파견 클래스에서 :

class ListeningClass { 
    function startListening(dispatcher:DispatchingClass) { 
     dispatcher.addEventListener("FOO", handleFoo); 
    } 

    function handleFoo(evt:Event) { 
     // do stuff 
    } 
} 
: 청취 클래스에서

class DispatchingClass extends Event Dispatcher { 
    function doSomething() { 
     // do stuff 
     dispatchEvent(new Event("FOO")); 
    } 
} 

EventDispatcher는 맞춤 이벤트와 잘 작동합니다. 어떤 이유로 청취 클래스가없는 당신의 파견 클래스의 인스턴스를 얻을 수없는 경우


, 당신은 글로벌 이벤트 방송을 할 수 있습니다. 기본적으로 EventDispatcher (또는 IEventDispatcher을 구현하는) 보편적으로 액세스 할 수있는 클래스를 만들고 이벤트를 수신하고 전달하는 모든 것에 이벤트를 전달합니다.

import flash.events.EventDispatcher; 

public class EventBroadcaster extends EventDispatcher { 
    private static var _instance:EventBroadcaster = new EventBroadcaster(); 

    public function EventBroadcaster() { 
     if (_instance != null) { 
      trace ("Error: an instance of EventBroadcaster() already exists."); 
     } 
    } 


    public static function getInstance():EventBroadcaster { 
     return EventBroadcaster._instance; 
    } 
} 

당신은 거의 같은 방법을 사용합니다 :

class DispatchingClass { 

    function doSomething() { 
     // do something 
     EventBroadcaster.getInstance().dispatchEvent(new Event("FOO")); 
    } 
} 

class ListeningClass { 
    function startListening() { 
     EventBroadcaster.getInstance().addEventListener("FOO", handleFoo); 
    } 

    function handleFoo(evt:Event) { 
     // do stuff 
    } 
} 

dispatchEvent()addEventListener()가 내장에서 불과 기능입니다을 여기

이벤트 브로드 캐스터의 베어 본 구현 EventDispatcher에 있습니다.

Event Broadcaster - Simple events solution...에는 이벤트 브로드 캐스터를 만드는 방법과 유용한 기능을 추가하는 방법에 대한 토론이 있습니다. 기사 Centralized Event Management in Actionscript 2.0에는 개념에 대한 좋은 소개가 있습니다.

관련 문제