鼠标单击addEventListener不起作用

时间:2014-10-15 06:39:20

标签: actionscript-3 flash actionscript

enter image description here这是我的代码:

    package
{
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.display.SimpleButton;

    import Game.*;

    public class GameController extends MovieClip
    {
        private var player:Jovia;
        private var speed:Number;
        private var gravity:Number;
        private var randomChance:Number;
        private var randomChance1:Number;
        private var ungos:Array;
        private var apples:Array;
        private var score:Number;       

        public function GameController()
        {

        }

        public function startMenu()
        {
            btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
            btnHelp.addEventListener(MouseEvent.CLICK, gotoHowToPlay);
            btnCredits.addEventListener(MouseEvent.CLICK, gotoCredits);
        }

        public function startGhost()
        {

        }

        public function startHelp()
        {
            btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
        }

        public function startCredits()
        {
            btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
        }

        public function startGameOver()
        {

            gotoAndStop("gameOver");
            txtScore.text = String(score);
            btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
            gravity = C.GRAVITY;
            speed = C.PLAYER_SPEED;
        }

        private function gotoCredits(evt:MouseEvent)
        {
            btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
            btnHelp.removeEventListener(MouseEvent.CLICK, gotoHowToPlay);
            btnCredits.removeEventListener(MouseEvent.CLICK, gotoCredits);
            gotoAndStop("credits");
        }

        private function gotoHowToPlay(evt:MouseEvent)
        {
            btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
            btnHelp.removeEventListener(MouseEvent.CLICK, gotoHowToPlay);
            btnCredits.removeEventListener(MouseEvent.CLICK, gotoCredits);
            gotoAndStop("howtoplay");
        }

        private function gotoGame(evt:MouseEvent)
        {
            btnPlay.removeEventListener(MouseEvent.CLICK, gotoGame);
            btnHelp.removeEventListener(MouseEvent.CLICK, gotoHowToPlay);
            btnCredits.removeEventListener(MouseEvent.CLICK, gotoCredits);
            gotoAndStop("game");
        }

        public function startGame()
        {
            speed = C.PLAYER_SPEED;
            gravity = C.GRAVITY;
            score = C.PLAYER_START_SCORE;
            randomChance = C.APPLE_SPAWN_CHANCE;
            randomChance1 = C.UNGO_SPAWN_CHANCE;
            ungos = new Array();
            apples = new Array();
            player = new Jovia();
            mcGameStage.addChild(player);           
            mcGameStage.addEventListener(Event.ENTER_FRAME,update);
        }       

        private function update(evt:Event)
        {
            //******************            
            //Handle User Input
            //******************
            var currMouseX = mouseX;
            var currMouseY = mouseY;
            var moveX = 0;
            var moveY = 0;
            if (currMouseX > player.x)
            {
                moveX = 1;
            }
            else if (currMouseX < player.x)
            {
                moveX = -1;
            }
            if (currMouseY > player.y)
            {
                moveY = 1;              
            }
            else if (currMouseY < player.y)
            {
                moveY = -1;             
            }           

            //******************
            //Handle Game Logic
            //******************
            //Handle new Player Position
            if (moveX > 0)
            {
                if (player.x + C.PLAYER_SPEED <= currMouseX)
                    player.x += C.PLAYER_SPEED;
            }
            else if (moveX < 0)
            {
                if (player.x - C.PLAYER_SPEED > currMouseX)
                    player.x -= C.PLAYER_SPEED;
            }           
            if (moveY > 0)
            {
                if (player.y + C.PLAYER_SPEED <= currMouseY)
                    player.y += C.PLAYER_SPEED;
            }
            else if (moveY < 0)
            {
                if (player.y - C.PLAYER_SPEED > currMouseY)
                    player.y -= C.PLAYER_SPEED; 
            }
            //Spawn Ungo
            if (Math.random() < randomChance1)
            {
                var newUngo = new Ungo();
                newUngo.x = Math.random() * C.UNGO_SPAWN_END_X + C.UNGO_SPAWN_START_X;

                newUngo.y = C.UNGO_START_Y;
                ungos.push(newUngo);

                mcGameStage.addChildAt(newUngo,0);
            }
            //Move Ungo
            for (var i = ungos.length-1; i >= 0; i--)
            {
                ungos[i].y += gravity;

                if (ungos[i].y > C.UNGO_END_Y)
                {
                    mcGameStage.removeChild(ungos[i]);
                    ungos.splice(i,1);
                }
            }   



            //Spawn new apples
            if (Math.random() < randomChance)
            {
                var newApple = new Apple();
                newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;

                newApple.y = C.APPLE_START_Y;
                apples.push(newApple);

                mcGameStage.addChildAt(newApple,0);
            }           
            //Move Apples
            for (var i = apples.length-1; i >= 0; i--)
            {
                apples[i].y += gravity;

                if (apples[i].y > C.APPLE_END_Y)
                {
                    mcGameStage.removeChild(apples[i]);
                    apples.splice(i,1);
                }
            }           

            //Check for collision
            var playerPoint = new Point(player.x, player.y);
            for (var i = apples.length-1; i >= 0; i--)
            {
                var applePoint:Point = new Point(apples[i].x, apples[i].y);
                if (Point.distance(applePoint,playerPoint) < C.HIT_TOLERANCE)
                {
                    //Register hit
                    score += C.SCORE_PER_APPLE;

                    mcGameStage.removeChild(apples[i]);
                    apples.splice(i,1);
                }
            }
            //ungo collision
            for (var i = ungos.length-1; i >= 0; i--)
            {
                var ungoPoint:Point = new Point(ungos[i].x, ungos[i].y);
                if (Point.distance(ungoPoint,playerPoint) < C.HIT_TOLERANCE)
                {
                    //Register hit

                    mcGameStage.removeChild(ungos[i]);
                    ungos.splice(i,1);
                    gotoAndStop("ghost");
                }
            }

            //******************
            //Handle Display
            //******************
            //Animate the player
            if (moveX > 0)
            {
                if (player.currentLabel != C.JOVIA_RIGHT)
                    player.gotoAndPlay(C.JOVIA_RIGHT);
            }
            else if (moveX < 0)
            {
                if (player.currentLabel != C.JOVIA_LEFT)
                    player.gotoAndPlay(C.JOVIA_LEFT);
            }           
            //Display new Score
            txtScore.text = String(score);
        }
    }
}

每帧代码:

菜单:

stop();
startMenu();

游戏:

stop();
startGame();

GAMEOVER:

stop();
startGameOver();

howtoplay:

stop();
startHelp();

现金:

stop();
startCredits();

幽灵:

stop();
var snd_scream = new SoundScream();
snd_scream.play();
    var myTimer:Timer = new Timer(2000, 1);
    myTimer.start();
    myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent){
       startGameOver();
    });

这里的问题是在函数startMenu()上,所有按钮都按预期工作,但在我的函数startHelp()上按钮根本没有反应。我在这里做错了什么?任何人都可以指出它吗?按钮也不适用于startCredits()startGameOver()

1 个答案:

答案 0 :(得分:0)

我能够通过删除所有removeEventListener并删除函数gotoCredits和gotoHowToPlay中的addEventListener来解决这个问题。但这是一种正确的做事方式,因为当你添加一个监听器时,你不应该删除它们吗?

谢谢