bragboy
bragboy

Reputation: 35542

What I might be doing wrong in this code?

I am relatively new to flash. I am trying to create a square grid and add it to the movie. When I open the actionscript panel by pressing F9 and when I type the following code,

var square:SquareClip = new SquareClip();
addChild(square);

Things are working fine (the squareclip is appearing in the movie).

Instead when I do this however, I deleted the above code and just create a new instance of Main,

new Main

and inside Main.as

package{
    //----
    //IMPORT
    //
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    //Class creation
    public class Main extends MovieClip {
        //PROPERTIES

        public function Main():void {           
            layout_in_grid();
        }

        private function layout_in_grid():void{
            trace("layout_in_grid");

            //create a new Square
            var square:SquareClip = new SquareClip();
            addChild(square);

            trace("Square added");
        }
    }
}

And when I run the code, my square is not coming. I am doing something wrong very basically. Please help me.

Upvotes: 0

Views: 74

Answers (2)

Glycerine
Glycerine

Reputation: 7347

@Mattias is correct. But you should set this as the Document Class as he suggested - When you've selected the stage, in the properties there will be an input box allowing you to enter the name of the class.

If your file is in the same location as the FLA and called 'Main.as' you enter in the box:

Main

If the file is within a folder structure e.g. com/company/projects/Main.as - enter:

com.company.projects.Main

--

Kudos on learning the OOP way!

Upvotes: 0

Mattias
Mattias

Reputation: 3907

You need to add Main to the displaylist:

var myMain : Main = new Main();
addChild(myMain);

You could also set Main as your document class.

Upvotes: 1

Related Questions