Jeppe Strøm
Jeppe Strøm

Reputation: 543

addChild error when not from Main.as

So I have Main.as and Helpers.as

I've made a function who adds children to stage but I get this error: "Main.as 1180: Call to a possibly undefined method addChildToStage"

So here's the code:

Main.as

package 
{

import flash.geom.*;
import flash.events.*;
import flash.display.*;
import flash.media.*;
import flash.text.*;
import flash.net.URLRequest;
import flash.utils.*;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;

public class Main extends Sprite 
{

        private var bus:Bus = new Bus();

        public function Main() 
        {

           Helpers.addChildToStage(bus, 176, 350, 0, 1)

        }

    }


}

Helpers.as

package
{
import flash.geom.*;
import flash.events.*;
import flash.display.*;
import flash.media.*;
import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.ui.Keyboard;


public class Helpers extends Sprite
{

    public function Helpers()
    {   


    }

    public static function addChildToStage(elem, stageX:int, stageY:int, depth:int, opacity:int ) {

        if (elem) 
        {
            addChild(elem)
            elem.x = stageX;
            elem.y = stageY;
            elem.alpha = opacity;

            switch(depth)
            {
                case 0:
                    depth = 0; // sendToBack
                    break;

                case 1:
                    depth = numChildren - 1; // bringToFront
                    break;
                default:
            }

            setChildIndex(elem, depth);
        }
    }

}

Upvotes: 0

Views: 123

Answers (1)

Fabricio
Fabricio

Reputation: 7935

  1. in Helpers.as you didn't close the } for your package statement.

  2. addChildToStage is a static function, so that addChild, numChildren and setChildIndex will not work inside it.

:)

Read about static method here and here.

Upvotes: 2

Related Questions