[haXe] casting attached MovieClips

Yariv Sadan yarivvv at gmail.com
Wed Apr 26 05:09:20 CEST 2006


Hi List,

I would like to know if there's some way to automate the casting of
instances of classes extending MovieClip when those instances are
created using MovieClip.attachMovie(). For instance, is it possible to
write code such as:

class MyClip extends MovieClip {
}

class Test {
  public static void doAttach(mc:MovieClip, className:Dynamic):className {
    return cast(mc.attachMovie(className + "", "foo",
mc.getNextHighestDepth(), null), className);
  }

 public static main() {
    var clip:MyClip = doAttach(flash.Lib.current, MyClip);
 }
}


I know the previous example doesn't compile (the attachMovie() call is
ok, but the cast fails), but is there any better way of doing this
rather than casting everything manually (without sacrifing type
safety)? I guess I'm looking for a mechanism similar to C++ function
templates, where one could write (hypothetically)

public static void doAttach<T>(mc:MovieClip):T {
  return cast(mc.attachMovie(T + "", "foo", mc.getNextHighestDepth(), null), T);
}

public static main() {
  var clip:MyClip = doAttach<MyClip>(mc);
}

Yariv



More information about the Haxe mailing list