PDA

View Full Version : where are classes born?



oldarney
February 13th, 2008, 05:22 PM
i got some code and a class and i want the class to access a movieclip residing in the same place that the class instance was created. how can i do this? parent ain't work in classes, neither does root.

McGuffin
February 13th, 2008, 05:58 PM
Pass an instance of the parent to the created class and you'll have able to access it's properties



var cc:CustomClass = new CustomClass(this);


and your class' constructor function becomes:



// asumes the parent is a Sprite
public function CustomClass(par:Sprite) {
_parent = par;
trace(_parent.x);
trace(_parent.getChildByName("theOtherChild"));
}


root and parent properties in DisplayObject classes only become available once they're added to the display list:



public function CustomClass() {
addEventListener(Event.ADDED_TO_STAGE, onAdd);
}

public function onAdd(e:Event):void {
trace(this.parent, 'is now available!');
}




var cc:CustomClass = new CustomClass();
// cc.parent is not set, it's null
this.addChild(cc);
// cc.parent is now set to the parent object (this) and the ADDED_TO_STAGE event is fired if the parent object is on the display list

oldarney
February 13th, 2008, 06:29 PM
thats as2 code witch dosent seem to work on as3 i get:

1135: Syntax error: function as not a valid type.

McGuffin
February 13th, 2008, 06:39 PM
It's not AS2. This has to go inside your classes, I wasn't typing that code out for the sake of brevity....



package {
import flash.display.Sprite;
import flash.events.Event;

public class CustomEvent extends Sprite {

public function CustomClass() {
addEventListener(Event.ADDED_TO_STAGE, onAdd);
}

public function onAdd(e:Event):void {
trace(this.parent, 'is now available!');
}
}
}


You can't expect everything to be handed to you on a gold-plated platter, now can you? ;)

oldarney
February 13th, 2008, 06:51 PM
i was closing up, then opened the files back up to add your code. when i notice the following


var parent:

@#$%&

i wast sure what to put there initially so i just left it that way until i finished adapting my script. when i finished, i had completely forgotten about that. THANK YOU

McGuffin
February 13th, 2008, 06:54 PM
Ugh, I hate it when I do that. No probs mate, best of luck :)

lauraegg
August 5th, 2010, 01:52 PM
How do I deal with this if the class is the document class and I need to get a stage (root) variable?
Thanks, I ve seen examples but donīt get it

Shaedo
August 5th, 2010, 02:22 PM
@ lauraegg
I think that might have been covered here (although might not be): http://www.kirupa.com/forum/showthread.php?t=352290

If it really is your document class then you can refer to the stage immediately eg

trace(stage);//[object Stage]
If it is not your document class then keep in mind that ....


root and parent properties in DisplayObject classes only become available once they're added to the display list:



public function CustomClass() {
addEventListener(Event.ADDED_TO_STAGE, onAdd);
}

public function onAdd(e:Event):void {
trace(this.parent, 'is now available!');
}




var cc:CustomClass = new CustomClass();
// cc.parent is not set, it's null
this.addChild(cc);
// cc.parent is now set to the parent object (this) and the ADDED_TO_STAGE event is fired if the parent object is on the display list


I'll include this link again because it's such an important one and people seem to be confused between root and stage:
http://www.kirupa.com/forum/showthread.php?p=2129548#post2129548