0

I'm kind of new to adding using classes in AS3, so far i've just been doing everything on frame1 of the movie. I figured I should eventually learn classes so here goes :) When I add objects onto the screen, I like to group them together in container objects for use later. So I have a board of Hex's that I'm building, and I'm trying to get them into a MovieClip named hexContainer that I have places on the stage. Now if I was doing this code like I normally would, I would just do hexContainer.addChild(tempHex). However, this is throwing me an error 1120.

My class code is as follows:

package  
{
import flash.display.MovieClip
import Hex
import flash.display.Stage

public class Boards extends Hex
{
    public function buildBoardOne()
    {
        for(var i:int = 1; i <= 5; i++)
        {
            var tempHex:Hex = new Hex();
            tempHex.x = 100;
            tempHex.y = 100;
            hexContainer.addChild(tempHex);
        }
    }

}

}

I did in the beginning just have these added to the Stage and was getting an error when I did that, that's why the import statement is there. I had checked on google to figure out why and that's what they said to do.

Now, when I added these to the stage it worked fine. I could get my hex's and manipulate them and we partied and it was a great time. Now that I'm trying to put them into the container movie clip they are rather angry at me and I just can't appease them :p

Any help you guys could give me would be greatly appreciated.

Edited code to test out what okayGraphics suggested:

package  
{
    import flash.display.MovieClip
    import Hex
    import flash.display.Stage

    public class Boards extends Hex
    {
        var hexContainer:MovieClip = new MovieClip();
        stage.addChild(hexContainer);

        public function buildBoardOne()
        {
            for(var i:int = 1; i <= 5; i++)
            {
                var tempHex:Hex = new Hex();
                tempHex.x = 100;
                tempHex.y = 100;
                stage.addChild(tempHex);
            }
        }

    }

}
  • 3
    A suggestion I can make is that when you start programming purely in classes, you forget about the stage and its content and instead work entirely with ActionScript. This will reduce confusion greatly. Use the document class as your main entry point for graphics (because it must extend Sprite or MovieClip). – Marty Jun 03 '12 at 23:27
  • I'm not exactly sure how to go around that. Not sure what a document class is either >.< If you wouldn't mind expanding or telling me what you mean I'd really appreciate it :) and thanks for your help – Tiffany Rehsif Jun 04 '12 at 18:34

1 Answers1

1

You are getting an 1120 error because hexContainer is not defined in this package. You either (1)need to declare var hexContainer = [your_reference_here] before you try to add children to it,

or

(2)you can just add the hexes as children to the Boards class, then add that to your hexContainer. instead of hexContainer.addChild(TempHex); just put addChild(TempHex);

There's lots of other ways to do it too, but I think these two are the most straightforward approaches.

okayGraphics
  • 375
  • 2
  • 10
  • @ okayGraphics herm this is going to sound silly, but I'm not exactly sure how to go about it that way. After your answer I did try adding in: herm, cant get the code to show here I'll post it in my original post. Well, I tried adding that in but I'm getting 1120 for stage, and 1120 for hexContainer. if I comment out the stage.addChild(hexContainer); then I no longer get the 1120 for stage (even though I have stage.addChild(tempHex) which is also confusing the hex out of me – Tiffany Rehsif Jun 04 '12 at 18:36