-2

I have my main class where I add EventLisitener to Sprites in other classes Like so:

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

    addChild(Menu.menuBackground);
    addChild(Menu.startButton);
    //Adds event Listener to Menu's Sprite startButton.
    Menu.startButton.addEventListener(MouseEvent.CLICK, startGame);
}

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

This all works Fine and Dandy but later I try to do it again, the same way. But, clicking on the sprites does nothing. Here is the full Main Class code. Along with the code for the other three Sprite classes.

Full Main:

package {

    //Other Files
    import Menu;
    import CrossHair;
    import Birds;

    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.ui.Mouse;

    public class Main extends Sprite {

        //Game values
        public static var gameWidth:int = 750;
        public static var gameHeight:int = 750;

        [Embed (source = "lib/background.png")]
        public var backgroundClass:Class;
        public var background:Bitmap = new backgroundClass();

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

            addChild(Menu.menuBackground);
            addChild(Menu.startButton);
            Menu.startButton.addEventListener(MouseEvent.CLICK, startGame);
        }

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

        //Function starts game
        public function startGame(Evt:Event):void {
            Mouse.hide(); //Hides Mouse

            removeChild(Menu.startButton); //Get rid of startButton
            Menu.startButton.removeEventListener(MouseEvent.CLICK, startGame);
            reRender();

            //Add eventListiners
            addEventListener(Event.ENTER_FRAME, update);
            Birds.bird.addEventListener(MouseEvent.CLICK, Birds.shot);
            EnterShopButton.shopButton.addEventListener(MouseEvent.CLICK, enterShop);
        }

        public function reRender():void {
            addChild(background); //Add background
            addChild(Birds.bird); //Add birds.
            addChild(EnterShopButton.shopButton); //Add UpgradeMenuButton
            addChild(CrossHair.crossHair); //Add crosshair
        }

        public function enterShop():void {
            stage.removeChildren(); //Removes all children from stage.
        }

        public function update(evt:Event):void {
            Birds.update();

            CrossHair.crossHair.x = mouseX - (CrossHair.crossHairImg.width / 2);
            CrossHair.crossHair.y = mouseY - (CrossHair.crossHairImg.height / 2);
        }


    }

}

Event Lisitners:

package {

    //Other files
    import flash.events.Event;
    import Main;

    import flash.display.Sprite;
    import flash.display.Bitmap;

    public class Menu extends Sprite {

        //create menbu background Bitmap
        [Embed (source = "lib/menubackground.png")]
        public static var menuBackgroundClass:Class;
        public static var menuBackground:Bitmap = new menuBackgroundClass();

        //create startButton Bitmap
        [Embed (source = "lib/menustartbutton.png")]
        public static var startButtonClass:Class;
        public static var startButtonImg:Bitmap = new startButtonClass();
        public static var startButton:Sprite = new Sprite();

        //Set startButton's values
        startButton.addChild(startButtonImg);
        startButton.x = (Main.gameWidth / 2) - (startButtonImg.width / 2);
        startButton.y = (Main.gameHeight / 2) - (startButtonImg.height / 2);
    }
}

package {

    //Other files
    import Main;

    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.events.MouseEvent;

    public class Birds extends Sprite {

        public static var xSpeed:int = 10;
        public static var ySpeed:int = 10;
        public static var dead:Boolean = false;

        //Create bird Sprite
        [Embed (source = "lib/bird.png")]
        public static var birdClass:Class;
        [Embed (source = "lib/birdead.png")]
        public static var deadBirdClass:Class;
        public static var birdImg:Bitmap = new birdClass();
        public static var deadBirdImg:Bitmap = new deadBirdClass();
        public static var bird:Sprite = new Sprite();

        //Sets Sprite's values
        bird.addChild(birdImg);
        bird.buttonMode = true;
        bird.x = 0;
        bird.y = 0;

        public static function update():void {
            bird.x += Math.random() * xSpeed;
            bird.y += Math.random() * ySpeed;
            if (!dead) {
                if (bird.x >= (Main.gameWidth - birdImg.width) || bird.x <= 0) {
                    xSpeed = xSpeed * -1;
                }
                if (bird.y >= (Main.gameHeight - birdImg.height) || bird.y <= 0) {
                    ySpeed = ySpeed * -1;
                }
            } else {
                if (bird.y > (Main.gameHeight - deadBirdImg.height)) {
                    resetBird();
                }
            }
        }

        public static function shot(evt:MouseEvent):void {
            if (!dead) {
                bird.removeChild(birdImg);
                bird.addChild(deadBirdImg);
                dead = true;
                xSpeed = 0;
                ySpeed = 50;
            }
        }

        public static function resetBird():void {
            bird.removeChild(deadBirdImg);
            bird.addChild(birdImg);
            dead = false;
            bird.x = 0
            bird.y = 0;
            xSpeed = 10;
            ySpeed = 10;
        }

    }

}


package {

    //Other Files
    import Main;

    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class EnterShopButton extends Sprite{

        //Create crossHair
        [Embed (source = "lib/shopbutton.png")]
        public static var shopButtonClass:Class;
        public static var shopButtonImg:Bitmap = new shopButtonClass();
        public static var shopButton:Sprite = new Sprite();

        //Set CrossHair's values
        shopButton.addChild(shopButtonImg);
        shopButton.buttonMode = true;
        shopButton.x = Main.gameWidth - shopButtonImg.width;
        shopButton.y = Main.gameHeight - shopButtonImg.height;

    }

}
Elgin Beloy
  • 93
  • 1
  • 6
  • Seeing your other question (http://stackoverflow.com/questions/34256441/as3-debugger-stops-responding-while-trying-to-load-image-into-sprite-using-loade), it seems that you haven't really picked up on the answer there. Don't expect people to keep on helping you when you keep doing the same thing, and expecting it to work all of a sudden. I'd suggest you revise your code according to the answer on your other question, if that gives you any issues, feel free to post them on SO, and people will be glad to help you. Good luck! – Gerrit Bertier Dec 15 '15 at 09:24

1 Answers1

0

Same problem, same answer. Your previous question was answered the same way but you still persist on doing the exact same thing and getting into the exact same problem.

Stop trying to run code in static scope! addChild can't work in that scope and any other type of code.

Not sure why I was voted down. There's not much to answer here since the same problem with the same user was answered in another post.

This is really not difficult, user wants to run code at class definition scope. At class definition scope you can instantiate variable yes but that's all you can do. User wants to run code and logic at that scope and this is just completely ignored by the compiler. This was already pointed out to him in his previous question and he was even given the advice to stop trying to run code at that scope to avoid further problem. This is really simple to understand:

    [Embed (source = "lib/menubackground.png")]
    public static var menuBackgroundClass:Class;
    public static var menuBackground:Bitmap = new menuBackgroundClass();
    //instantiation is allowed at that scope and will work

    //create startButton Bitmap
    [Embed (source = "lib/menustartbutton.png")]
    public static var startButtonClass:Class;
    public static var startButtonImg:Bitmap = new startButtonClass();
    //instantiation is allowed at that scope and will work
    public static var startButton:Sprite = new Sprite();
    //instantiation is allowed at that scope and will work

    //COMPILER WILL IGNORE AT THAT SCOPE ANY CODE THAT IS NOT INSTANTIATION
    //SO ALL FOLLOWING LINES ARE IGNORED AND PRODUCE NO RESULT
    //EVEN TRACING WOULD NOT WORK HERE
    // (Main.gameWidth / 2) WILL NOT BE CALCULATED AND DOES NOT PRODUCE ANY VALUE
    // startButton.x WILL NOT BE SET AND WILL STAY AT DEFAULT VALUE 0
    // FINALLY startButton WILL NOT addChild ANYTHING.
    //ANY FORM OF CODE LOGIC IS IGNORED AT THAT SCOPE AND WILL NEVER PRODUCE ANY RESULT
    startButton.addChild(startButtonImg);
    startButton.x = (Main.gameWidth / 2) - (startButtonImg.width / 2);
    startButton.y = (Main.gameHeight / 2) - (startButtonImg.height / 2);
BotMaster
  • 2,233
  • 1
  • 13
  • 16
  • Please compile and check before answering. If you were to read my answer you would see that when compiled the startButton works perfectly. The x, and y attributes work perfectly. These are not my problems. The static state of these variables affects nothing, you can see this when compiled. Please re-read my problem, and answer correctly. – Elgin Beloy Dec 15 '15 at 23:48