2

I am creating a game in ActionScript. I am running into problems with object oriented programming in actionscript. I have a game_fla which hosts the library components of the game. The one which is causing problems is a splash movie clip. Within this movie clip I have a few layers that animate and load a logo and two buttons. In the document class game.as, I have the following code:

package{
import flash.display.MovieClip;
public class the_game extends MovieClip {
    public var splash_screen:splash;
    public var play_screen:the_game_itself;
    public var how_to_play_screen:how_to_play;



    public function the_game() {
        show_splash();
    }

    public function show_splash() {
        splash_screen = new splash(this);
        addChild(splash_screen);
    }

    public function play_the_game() {
        play_screen = new the_game_itself(this,level);
        remove_splash();
        addChild(play_screen);
    }
etc..

This obviously refers to a splash.as file which holds the information regarding the splash components. This is the code for splash.as:

package {
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.events.MouseEvent;
    public class splash extends MovieClip {
    public var main_class:the_game;
    public function splash(passed_class:the_game) {
        main_class = passed_class;
        play_btn.addEventListener(MouseEvent.CLICK, playGame);
        howToPlay_btn.addEventListener(MouseEvent.CLICK, howToPlay);

    }

    public function playGame(event:MouseEvent):void{
        main_class.play_the_game();
    }

    public function howToPlay(event:MouseEvent):void{
        main_class.how_to_play();
    }

}

}

To my point! The problem I am having is that when I run the game.fla file, I am getting a compiler error for the splash.as file saying "1120:Access of undefined property play_btn and howToPlay_btn". These buttons like I mentioned are within the movie clip splash_mc. (all have instance names etc..) Just not sure where I am going wrong? By the way, I originally had the as files using Sprite as opposed to Movie Clip but neither work anyways.

HELP? Please? Anyone?

  • 1
    Are the buttons on stage on the first frame of `splash_mc`? If they're on a later frame, the `splash()` constructor cannot access them. – David Mear Mar 07 '13 at 21:11

1 Answers1

0

Like in Life, its bad OOP to let the child tell the parent what to do. It should only start events and a parent can react if it has to. Otherwise you create dependancies.

you do something like this:

//in the parent
public function show_splash() {
        splash_screen = new splash();//get rid of this, remember to delete from main constructor
        splash_screen.addEventListener("PLAY_GAME", play_the_game);//add listener
        addChild(splash_screen);
    }


//in the child you just dispatch the event when you need it
public function playGame(event:MouseEvent):void{
        dispatchEvent(new Event("PLAY_GAME"));
    }

then when that works you do the same with how_to_play

You only use MovieClips if you need frames, otherwise use a Sprite. Also, sometimes you cant get around passing the parent as an argument, but then you pass it as a DisplayObjectContainer or even better give it a setter.

M4tchB0X3r
  • 1,531
  • 1
  • 15
  • 28
  • Thanks for your answers and comments, I figured out what the problem was. I had a small animation at the beginning of the splash_mc of the logo and buttons zooming in. I would presume that the reason for the problem was because I did not have these buttons available at frame 1 of the movie clip. Two days that cost me...! – user2145747 Mar 08 '13 at 12:45