[haXe] casting attached MovieClips

Yariv Sadan yarivvv at gmail.com
Thu Apr 27 01:46:02 CEST 2006


thanks!

On 4/26/06, Nicolas Cannasse <ncannasse at motion-twin.com> wrote:
> > 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);
> > }
>
> That's not so easy. In particular, the type T can't be used as an
> expression in method body right now. However, the following should work:
>
> public static function doAttach<T>(mc:MovieClip,cl : Dynamic) : T {
>     var depth = mc.getNextHighestDepth();
>     var sub = mc.attachMovie(cl.__name__.join("."),"foo"+depth,depth);
>     if( !Std.is(sub,cl) )
>         throw "doAttach failed";
>     return untyped sub;
> }
>
> Then :
>
> doAttach<MyClip>(root,MyClip)
>
> Nicolas
>
> --
> haXe - an open source web programming language
> http://haxe.org
>



More information about the Haxe mailing list