views:

100

answers:

1

Hi to everyone,

I have been trying to add a movieclip to my .fla file from a class.

my main fla file: main.fla actionscript file: script.as

they stay in the same directory.

script.as file as follows:

package  {
import flash.display.*

public class MyClass extends MovieClip
{
    public var target:MovieClip;

    public function MyClass() 
    { 
        init();

    }

    public function init() //if this method will be runed only inside class it should be private not public
    {
        trace('created');

    }
    public function create()
    {
        target = new myfan();
        target.x = 400;
        target.y = 50;
        stage.addChild(target);


    }
}

}

I use create function to create add the movieclip from main.fla file.

var trying:MyClass = new MyClass();
trying.create();

The output give "created" but i connot see "myfan" movieclip inside the outputted swf file.

I have been searching all the day but could not find very much.

How can i add a movieclip from a class file?

Thanks for your kind help.

+2  A: 

I'm a bit confused as to how you are attaching the script.as to the swf. Classes in AS3 must be in a file with the exact same name as the class name - so your file should be named MyClass.as as a first point.

The second question is what is myfan exactly? Is it a symbol in your library? What does that symbol contain? What happens if you drag the symbol onto the stage instead of trying to instantiate it using ActionScript?

Finally, if you are instantiating MyClass where are you doing so? On the first frame of the movie within the timeline? I notice that you aren't adding the variable trying onto the display list at any time .... that would mean the variable stage might be null. Try adding:

public function create()
{
    target = new myfan();
    target.x = 400;
    target.y = 50;
    trace("Stage here?:", stage);
    stage.addChild(target);
}

To see if you get anything.

James Fassett
Thank you James, i have figured it out.the problem was, as you indicated, stage is null. in order to solve this, you have to add the varible to display list:var trying:MyClass = new MyClass();this.addChild(trying);trying.create();after this, you can add movieClips inside the class.
isa