Have been trying to understand this problem for two weeks.
When I use addChild(movieclip) in the Main.as file, it works. When I try to use it in a function in a separate class file it does not work.
I've read about 40 different explanations, but all of them failed to explain it. I tried about 10 different suggestions, various code, but none of them worked.
Here is the code. This code is a separate file called Behavior. The main code is in Main.as.
package {
import flash.utils.Timer;
import flash.events.TimerEvent;
import Box2D.Common.Math.b2Vec2;
import flash.geom.Matrix;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.geom.Point;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flashx.textLayout.elements.ParagraphElement;
import flash.display.Loader;
import flash.display.Stage;
public class Behavior extends MovieClip {
public function Behavior() {
// didn't work. someone else's idea.
Behavior._behavior=this;
}
static public function explodeOrb(explosion:MovieClip)
{
var myPoint:Point = new Point();
myPoint.x = explosion.x;
myPoint.y = explosion.y;
explosion.parent.removeChild(explosion);
Main._balls.push(new Ball(Main.createExplosion(myPoint.x, myPoint.y, Constants.RADIUS_EXPLOSION), new Explosion(), Constants.WALL));
var ballCurrent:Ball = Main._balls[Main._balls.length-1];
//var obj:Behavior = new Behavior();
addChild(ballCurrent); // FAIL FAIL FAIL FAIL FAIL
}
Of course it says addChild is not a property of blah blah, even though it just was able to removeChild from the same object and the object is defined.
I would just call the function from the main class with clip = Behavior.explodeOrb because then addChild DOES work.
The functions have to be static, since you can't call functions from other AS files. And I need to call them when I want them and not worry about a bunch of segregation and obfuscation.