0
votes

I'm a complete noob to flash and know I'm just missing something obvious here.

I have designed my screens as a movieclip and have buttons in each of the movieclips which link the menu screens together .. everything worked fine using the timeline and code snippets.

Now, I'm trying to do it in code, I'm having some problems:

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Main extends MovieClip {

        public function Main() {
            // define variable main as the movieclip main
            var main = new Main_menu();
            var instructions = new instructions();
            //add the movieclip to the stage
            addChild(main);
            // Tell the button to run function on_play.....
            Instructions_btn.addEventListener(MouseEvent.CLICK, on_play_button_clicked);
        }

        // what the button does
        public function on_play_button_clicked(event:MouseEvent) {
            addChild(instructions);
        }
    }
} 

I can get the movie clip Main to display and since ive added the buttons in the movieclip within flash they display fine and the cursor changes to a finger click icon :-)

What i cant seem to work out is how to get the button with class name: instructions_btn to add the screen : instructions when I click it.

Dead easy probably but I'm pulling my hair out!

1
Where does Instructions_btn come from?Ben

1 Answers

0
votes

You are using the same name for you variable instance "instructions" and for the class "instructions". This, obviously, cannot work. Stick to naming conventions: The first letter of a Class name should be upper case, that of an instance name lower case:

var instructions:Instructions = new Instructions();
addChild (instructions);

Also, you are declaring instructions as a local variable inside the constructor for Main, but try to add it to the stage in the event handler - when it is no longer available. If you want your variable to exist outside of a method's scope, declare it as a member:

public class Main extends MovieClip {
    private var instructions:Instructions;

    public function Main () {
        instructions = new Instructions();
        // ...
    }

    private function onPlayButtonClicked (ev:Event) : void {
        addChild (instructions);
    }
}