20 posts
  • Has been a member for 2-3 years
pctechtv says

I am learning AS3 and was wondering can a Class file contain a method to call it self? Kind of like:

function someMethod():void { addChild(this); }

Of course the method is in a package and class. My quick answer to my self is no, but I know I have a lot to learn.

3649 posts Community Moderator
  • Has been a member for 5-6 years
  • Contributed a Tutorial to a Tuts+ Site
  • Netherlands
  • Community Moderator
  • Microlancer Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
+4 more
Joost Volunteer moderator says

Basically what you’re implicitly saying is this.addChild(this);. That’s quite a paradoxial situation and would lead to an endless recursion ;) So somebody correct me if I’m wrong, but it seems to me that this is not something you might want to do.

You could always just try it out and see what happens, though :delicious:

535 posts
  • Beta Tester
  • Has been a member for 5-6 years
  • Sold between 1 000 and 5 000 dollars
dimumurray says

Ask yourself this, in a real world scenario, can any container (for example a teacup) contain itself? Highly improbable. The same applies when adding elements to the display list via the addChild() method. I strongly suspect that AS3 will bark at you (ie throw an exception) if you try that.

1657 posts
  • Exclusive Author
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
  • Sold between 50 000 and 100 000 dollars
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
+2 more
flashedge says

That would cause an infinite loop and probably your computer would crash, get fire and everybody dies.

1234 posts
  • Bought between 50 and 99 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 50 and 99 users
  • Sold between 50 000 and 100 000 dollars
  • United States
MBMedia says

In the scene graph each object has a property of a parent and list of children (or just parent if it’s not an object that can contain children). For an object to be it’s own parent would mean that it’s own concatenated transform data (such as it’s concatenatedMatrix) would be used as it’s parent concatenated transform data to combine with it’s local transform data, which would result in it infinitely applying any transform information, such as if you set the x to 10 it’s concatentated x value would be 10 * infinity. Basically, it would go against the idea of what forward kinematics (objects containing each other) is for, and I’d assume that if you attempted it it would throw either a runtime or compiler error saying not to do that.

Edit: looked it up, it’s error #2024, “An object cannot be added as a child of itself.”

by
by
by
by
by
by