2017-03-24 1 views
0

다른 유형의 여러 하위 개체가있는 스테이지가 있습니다. 무대에 이벤트 리스너가 있습니다. 다른 개체 유형에 대한 클릭을 처리 할 수 ​​있도록 마우스 클릭시 대상 하위 개체를 가져오고 싶습니다. 의 라인을 따라OpenFL에서 마우스 클릭 이벤트의 하위 대상 얻기

뭔가 :

stage.addEventListener(MouseEvent.CLICK, onClick); 

function onClick(e:Event):Void 
{ 
    var target = e.target 
    // if target type is Foo 
    //  target.aFooMethod(); 
    // else if target type is Bar 
    //  target.aBarMethod(); 

} 

무슨이 일을 올바른 방법? e.target 추적 올바른 개체 형식을 인쇄 할 수 있지만 모든 개체 메서드를 호출 할 수 없습니다.

액션 스크립트 3에서는 target.name을 사용할 수 있지만이 경우 null을 반환한다는 사실을 모호하게 기억합니다.

답변

1

귀하의 질문은 대부분 런타임 유형 식별 (RTTI) 및 유형 캐스팅에 관한 것 같습니다. Haxe는 이것을 처리하기 위해 TypeReflect 클래스의 많은 유틸리티를 제공합니다. 아마도

if (Std.is(target, Foo)) 
    (target:Foo).aFooMethod(); 
else if (Std.is(target, Bar)) 
    (target:Foo).aBarMethod(); 

또는 : 여기

if (Std.is(target, Foo)) { 
    var targetAsFoo:Foo = cast target; 
    targetAsFoo.aFooMethod(); 
} else if (Std.is(target, Bar)) { 
    var targetAsBar:Bar = cast target; 
    targetAsBar.aBarMethod(); 
} 

Std.is, 인터페이스를 포함하여 당신을 도울 수있는 유틸리티의 수를 보여주는 예입니다 특별히 원하는처럼 코드에 대한

, 그것은 보인다 , Type.getClass, 타입 캐스팅 등 : http://try.haxe.org/#3C192

코드는 다음과 같습니다 :

class Test { 
    static function main() { 

     function identify(tgt:Dynamic) 
     { 
      trace("1: "+tgt); 
      trace("2: "+Type.typeof(tgt)); 
      trace("3: "+Type.getClassName(tgt)); 
      trace("4: "+Type.getClass(Type.getClassName(tgt))); 
      trace("5: "+Std.is(tgt, Something)); 

      if (Std.is(tgt, Something)) { 
      // Can cast explicitly 
       var casted:Something = cast tgt; 
       trace("Got a Something named: "+casted.name); 
      } 

      if (Std.is(tgt, Something)) { 
      // Can cast implicitly 
       var casted:Something = untyped tgt; 
       trace("Got a Something named: "+casted.name); 
      } 

      if (Std.is(tgt, Something)) { 
      // Can cast in an inline style, (obj:Type) 
       trace("Got a Something named: "+(tgt:Something).name); 
      } 

      if (Std.is(tgt, IHasAName)) { 
      // Can cast to an interface, if you prefer 
       var i_casted:IHasAName = (tgt:IHasAName); 
       trace("Got a IHasAName named: "+i_casted.name); 
      } 

      // Can reflect to see if the name field exists: 
      if (Reflect.hasField(tgt, "name")) { 
       trace("tgt has a name: "+Reflect.field(tgt, "name")); 
      } 
      if (Reflect.hasField(tgt, "length")) { 
       trace("tgt has a length: "+Reflect.field(tgt, "length")); 
      } 
     } 

     trace("----------------------------------"); 
     trace("Calling identify with a Something:"); 
     trace("----------------------------------"); 
     var a = new Something("foo", 3); 
     identify(a); 

     trace("----------------------------------"); 
     trace("Calling identify with a String:"); 
     trace("----------------------------------"); 
     var b = "a string"; 
     identify(b); 

     trace("----------------------------------"); 
     trace("Calling identify with anonymous:"); 
     trace("----------------------------------"); 
     var c = { "name" : "anonymous" }; 
     identify(c); 

    } 
} 

class Something implements IHasAName 
{ 
    public var name:String; 
    public var length:Int; 
    public function new(name:String, length:Int) 
    { 
     this.name = name; 
     this.length = length; 
    } 
} 

interface IHasAName { 
    public var name:String; 
} 

출력은 다음과 같습니다 왠지 target.name를 사용하지만,이 경우 null을 리턴 할 수있는 액션 스크립트 3에서 기억

14:41:24:664 ---------------------------------- 
14:41:24:664 Calling identify with a Something: 
14:41:24:664 ---------------------------------- 
14:41:24:664 1: { name : foo, length : 3 } 
14:41:24:665 2: TClass({ __name__ : [Something] }) 
14:41:24:665 3: null 
14:41:24:665 4: null 
14:41:24:665 5: true 
14:41:24:665 Got a Something named: foo 
14:41:24:665 Got a Something named: foo 
14:41:24:665 Got a Something named: foo 
14:41:24:665 Got a IHasAName named: foo 
14:41:24:665 tgt has a name: foo 
14:41:24:665 tgt has a length: 3 
14:41:24:666 ---------------------------------- 
14:41:24:666 Calling identify with a String: 
14:41:24:666 ---------------------------------- 
14:41:24:666 1: a string 
14:41:24:666 2: TClass({ __name__ : [String] }) 
14:41:24:666 3: null 
14:41:24:666 4: null 
14:41:24:666 5: false 
14:41:24:666 tgt has a length: 8 
14:41:24:667 ---------------------------------- 
14:41:24:667 Calling identify with anonymous: 
14:41:24:667 ---------------------------------- 
14:41:24:667 1: { name : anonymous } 
14:41:24:667 2: TObject 
14:41:24:667 3: null 
14:41:24:668 4: null 
14:41:24:668 5: false 
14:41:24:668 tgt has a name: anonymous 

. AS3에서

, event.target는 AS3에서 그렇게 할 수있는 이유입니다, 모든 DisplayObjects have a .name property 유형 DisplayObject입니다합니다.

OpenFL의 Event.target은 특별히 디스플레이 클래스와 관련이없는 IEventDispatcher입니다.

0

무대에 "이름"값이 있습니까? 내 테스트에서 event.target.name을 사용하면 효과가 있습니다.

확인하려면 프레임 워크가 수동으로 지정되지 않은 경우에도 기본값을 설정하므로 event.target.id을 사용해보십시오.

Goodluck.

관련 문제