-1

I created a symbol (MovieClip) in my fla file and added it to the frame. Then i give it instance name at properties panel: "myMC"

Then tried to control it from my class file in directory: root\Test\MyClass.as

package Test {
    import flash.display.MovieClip;

    public class MyClass extends MovieClip{
        public function MyClass() {
            var myMC:MovieClip = getChildByName("myMC") as MovieClip;
            myMC.gotoAndStop(2);
        }
    }
}

After that, imported the class to fla and tried to run:

import Test.MyClass;
var LaunchMyClass:MyClass = new MyClass();

and get this error,

TypeError: Error #1009: Cannot access a property or method of a null object reference.

at Test::MyClass()
at Test_fla::MainTimeline/frame1()

Note that i'm trying to control a MovieClip already in the stage, not calling it from library.

rasitayaz
  • 400
  • 2
  • 16

1 Answers1

2

First of all, always post the Errormessages you get with your question. Makes things easier.

But now let's look at your script:

I created a symbol (MovieClip) in my fla file and added it to the frame. Then i add it as a child using addChild(myMC);

if you put it on the stage and gave the MovieClip an Instancename there's no need to call addChild.

in as3 it's convinient to start classnames with a capital letter, so instead of myClass, call it MyClass.

your package is missing the constructor function, the name for the function has to be the same name as the class' name and the .as filename.

the getChildByName() method expects a String, so you have to wrap myMc in quotes

package  {
    import flash.display.MovieClip;


    public class MyClass extends MovieClip {

        // Constructor
        public function MyClass() {
            // constructor code
            var myMC:MovieClip = getChildByName("myMC") as MovieClip;
            myMC.gotoAndStop(2);
        }
        // End Constructor
    }

}
Patang
  • 314
  • 2
  • 8
  • I tried what you say and it come up with another problem. Edited the post for detailed information. – rasitayaz Nov 11 '17 at 15:36
  • well ok, i thought you use MyClass.as as the document class of your fla. what exactly are you trying to achieve and what is the purpose of MyClass? @theRaggedyMan – Patang Nov 11 '17 at 16:40
  • I'm trying to control a movieclip in the scene using MyClass. I have a complex game with many movieclips init. I used to control them from fla file but the code got chaotic in time so i want to seperate my code using classes. – rasitayaz Nov 11 '17 at 18:32