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.


    static public function explodeOrb(explosion:MovieClip)

        var myPoint:Point = new Point();
        myPoint.x = explosion.x;
        myPoint.y = explosion.y;


        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.


I'm not familiar with flash/as3.

This said, I fail to see what instance would be the parent to the child you're adding with addChild in a static method.

Here is the description from the documentation

Adds a child DisplayObject instance to this DisplayObjectContainer instance. The child is added to the front (top) of all other children in this DisplayObjectContainer instance. (To add a child to a specific index position, use the addChildAt() method.)

Your addChild does not work because you're in a static method, and there is no static version of the addChild method.

To fix this issue I see this option: Pass in the object that should receive the new child (Main?) and call addChild on that object (myObject.addChild(ballCurrent);).

From what I understand from your question, this is a patch that could work for you. We're lacking a bit of information on what you're trying to achieve in there.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.