Domanda

I am following a tutorial (http://flashdeveloptutorial.blogspot.com/2011/08/chapter-2.html) for AS3 using flash develop to create a pong clone and am having trouble with displaying a main menu. I previously had a functioning prototype with a moving paddle so I believe the error(s) is in my revised(or new) Main, MainMenu, PongGame or CustomEvents file(s). When I run the code as instructed through the tutorial I receive the error ; access of undefined property menu in my Main.as file:

 package 
    {
        import flash.display.Sprite;
    import flash.events.Event;

    /**
     * ...
     * @author 
     */
    public class Main extends Sprite 
    {

        private var game:PongGame;
        ****private var menu:MainMenu;****

    public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

        }

        private function buildMenu():void {
            menu = new MainMenu();
            addChild(menu);
            menu.addEventListener(CustomEvents.LAUNCH_GAME, startGame, false, 0, true);
        }

        private function startGame(e:CustomEvents):void {
            removeChild(menu);
            menu.removeEventListener(CustomEvents.LAUNCH_GAME, startGame);
            menu = null;
            game = new PongGame();
            addChild(game);
        }
    }
    }

So I added the "private var menu:MainMenu;" section but I do not know what class/ .as file to reference? or how to define var menu:

Sorry for the large amount of attached code. I did not change paddle.as which is the longest file.

assets.as

package 

{
    public class Assets  
    {
        [Embed (source = '../lib/paddle.png')] public static const Pad:Class;
    public function Assets(): void {

        }
    }

}

Paddle.as

package 

{
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    public class Paddle extends Sprite {
        private var pic:Bitmap = new Assets.Pad();
        public function Paddle():void {
            addEventListener(Event.ADDED_TO_STAGE, go);
        }
        private function die(e:Event):void {
            removeChild(pic);
            pic = null;
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
            stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
            removeEventListener(Event.ENTER_FRAME, enterFrame);
            removeEventListener(Event.REMOVED_FROM_STAGE, die);
        }

    private function go(e:Event) : void {
        removeEventListener(Event.ADDED_TO_STAGE, go);
        addChild(pic); 
            y = stage.stageHeight - pic.height;
            x = stage.stageWidth * .5 - pic.width * .5;
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, false, 0, true);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler, false, 0, true);
        addEventListener(Event.ENTER_FRAME, enterFrame, false, 0, true);
        addEventListener(Event.REMOVED_FROM_STAGE, die);
        }

    private var movingLeft:Boolean; 
    private var movingRight:Boolean;

    private function enterFrame(e:Event):void {
        if (movingLeft) {
            if(x-speed >= 0) {
                x -= speed;
        }
            else {
                x = 0;
            }
        }
        else if (movingRight) {
            if (x + speed + pic.width <= 600) {
                x += speed;
            }
            else {
                x = 600 - pic.width;
            }
        }
    }
    private var speed:int = 10;



    private function keyDownHandler(e:KeyboardEvent):void {
        if (e.keyCode == 38 || e.keyCode == 87) {
        if  (!movingLeft) {
            movingLeft = true;
            }
        }
        else if (e.keyCode == 40 || e.keyCode == 83) {
            if (!movingRight) {
                movingRight = true;
                }
            }
        }


    private function keyUpHandler(e:KeyboardEvent):void {
        if (e.keyCode == 38 || e.keyCode == 87) {
            if (movingLeft) {
                movingLeft = false;
            }
        }
        if (e.keyCode == 40 || e.keyCode == 83) {
            if (movingRight) {
                movingRight = false;
            }
        }

    }

    }
    }

mainmenu.as

package 

{
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.MouseEvent;


    public class MainMenu extends Sprite
    {
        private var pongButton:Sprite;

        public function MainMenu():void{
            addEventListener(Event.ADDED_TO_STAGE, go);
        }

        private function go(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, go);
            pongButton = item("Play", 20, 30, launchGame, 0xFF0000);
            addChild(pongButton);
        }

        private function launchGame(e:MouseEvent):void {
            dispatchEvent(new CustomEvents(CustomEvents.LAUNCH_GAME));
        }

        private function item(buttonText:String, X:int, Y:int, Funct:Function, txtColor:uint = 0xFFFFFF):Sprite {
            var item:Sprite = new Sprite();
            item.graphics.beginFill(0);
            item.graphics.lineStyle(1, txtColor, .5);
            item.graphics.drawRect(0, 0, 250, 30);
            var myText:TextField = new TextField();
            myText.selectable = false;
            myText.width = 250;
            myText.height = 30;
            item.addChild(myText);
            myText.autoSize = "center";
            myText.text = buttonText;
            myText.textColor = txtColor;
            item.addEventListener(MouseEvent.CLICK, Funct);
            item.x = X;
            item.y = Y;
            return item;
        }

    }

}

PongGame.as

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class PongGame extends Sprite
    {
        private var paddle:Paddle;

        public function PongGame():void {
            addEventListener(Event.ADDED_TO_STAGE, go);
        }

        private function go(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, go);
            paddle = new Paddle();

            addChild(paddle);
        }
    }

}

CustomEvents.as

package 
{
    import flash.events.Event;

    public class  CustomEvents extends Event
    {
        public static const LAUNCH_GAME:String = "launch_game";

        public function CustomEvents(e:String):void {
            super(e);
        }

    }

}

I'm not sure where to go from here to fix this error. Any help would be appreciated.

È stato utile?

Soluzione

Although you've correctly added the menu property, the menu is never displayed because buildMenu() function is never called.

From your main class init(), call buildMenu() to instantiate and add menu to the stage:

Main.as

private function init(e:Event = null):void 
{
    removeEventListener(Event.ADDED_TO_STAGE, init);

    // entry point
    buildMenu();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top