简体   繁体   中英

Actionscript OOP

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM