code_junkie

Referencing back to the parent from a child object

Nov 10th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. _class:Class = getDefinitionByName("com.site.Class") as Class;
  2.  
  3. _class(parent.parent).function();
  4.  
  5. trace(Class);
  6.  
  7. var class:Class = new Class(this);
  8.  
  9. public function Class(objectRef:Object) {
  10. _parentRef = objectRef;
  11. }
  12.  
  13. import com.site.Class;
  14. com.site.Class.method();
  15.  
  16. //Child.as
  17. package
  18. {
  19. import flash.events.EventDispatcher;
  20. import flash.events.Event;
  21.  
  22. public class Child extends EventDispatcher
  23. {
  24. public function doSomething():void
  25. {
  26. var e:Event = new Event(Event.COMPLETE);
  27. dispatchEvent(e);
  28. }
  29.  
  30. public function foo():void
  31. {
  32. trace("foo");
  33. }
  34. }
  35. }
  36.  
  37.  
  38. //Parent.as
  39. package
  40. {
  41. import flash.display.Sprite;
  42. import flash.events.Event;
  43. public class Parent extends Sprite
  44. {
  45. private var child:Child;
  46. public function Parent():void
  47. {
  48. c = new Child();
  49. c.addEventListener(Event.COMPLETE, onComplete);
  50. c.foo();//traces foo
  51.  
  52. c.doSomething()
  53. }
  54.  
  55. public function onComplete(e:Event):void
  56. {
  57. trace("Child broadcast Event.COMPLETE");
  58. }
  59.  
  60. }
  61. }
  62.  
  63. private static const class:Class;
  64.  
  65. public static function setClass(_class:Class){
  66. class = _class;
  67. }
  68. public static function getClass(void):Class{
  69. return class;
  70. }
  71.  
  72. ParentClass(parent).parentFunction();
  73.  
  74. ParentClass(stage.getChildAt(0)).parentFunction();
Add Comment
Please, Sign In to add comment