Last active
May 5, 2016 20:56
-
-
Save jcward/8cc4ef697cdd8d11b760b72e0bed0fa6 to your computer and use it in GitHub Desktop.
An idea for Dynamic MovieClips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Note 1: you'll have to comment out DisplayObject's implements Dynamic and DisplayObjectContainer's resolve function | |
| // Note 2: this depends on abstract resolve: https://github.com/HaxeFoundation/haxe/issues/3753 | |
| // | |
| // I tested Haxe 3.3 which is supposed to have @:resolve for abstract, but it still didn't work | |
| // I must have done something wrong. :P | |
| package; | |
| import openfl.display.*; | |
| import openfl.Assets; | |
| class Main extends Sprite { | |
| public function new () { | |
| super (); | |
| var mc = new MovieClip(); | |
| addChild(mc); | |
| var c1 = new Sprite(); | |
| c1.name = "child1"; | |
| mc.addChild(c1); | |
| var c2 = new Sprite(); | |
| c2.name = "child2"; | |
| c1.addChild(c2); | |
| var leaf:Shape = new Shape(); | |
| leaf.name = "leaf"; | |
| leaf.graphics.beginFill(0xff0000); | |
| leaf.graphics.drawCircle(0,0,10); | |
| c2.addChild(leaf); | |
| // reference by name, through non-dynamic sprites: | |
| $type(mc); // MovieClip | |
| $type(mc.child1); // DynamicDisplayObject - should have a .child2 ??? | |
| mc.child1.child2.leaf.x = 100; | |
| mc.child1.child2.leaf.y = 100; | |
| } | |
| } | |
| class MovieClip extends Sprite implements Dynamic<DynamicDisplayObject> | |
| { | |
| public function new() { super(); } | |
| private function resolve (fieldName:String):DynamicDisplayObject { | |
| if (__children == null) return null; | |
| for (child in __children) { | |
| if (child.name == fieldName) { | |
| return child; | |
| } | |
| } | |
| return null; | |
| } | |
| } | |
| @:forward | |
| abstract DynamicDisplayObject(DisplayObject) to DisplayObject from DisplayObject | |
| { | |
| @:resolve | |
| function resolve(fieldName:String):DynamicDisplayObject | |
| { | |
| if (Std.is(this, DisplayObjectContainer)) { | |
| return cast(this, DisplayObjectContainer).getChildByName(fieldName); | |
| } | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment