-2
\$\begingroup\$

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.

\$\endgroup\$

closed as off-topic by Vaughan Hilts, Seth Battin, Josh May 11 '15 at 15:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Josh
If this question can be reworded to fit the rules in the help center, please edit the question.

1
\$\begingroup\$

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.

\$\endgroup\$

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