如何在游戏完成时从数组中删除事件侦听器和对象?

时间:2014-12-19 14:03:05

标签: actionscript-3

我写了一个拖放游戏,在大多数情况下,它的工作原理。 它运行并完成我需要它做的事情。 但是,我无法弄清楚两件事。 如何从屏幕上移除玩具对象并在游戏结束后重新添加它们。 如何在游戏开始后删除用于拖动/碰撞操作的事件侦听器。 目前,在显示得分后,您仍然可以放下玩具箱中的物品,即使显示游戏结束也会使分数上升。

有谁知道我做错了什么? 我很想知道这一点,但它让我发疯。 任何帮助都会很棒。

这是我的代码......

package  {

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.Font;
import flash.filters.GlowFilter;


public class MainGame extends MovieClip {

        const BG_SPEED:int = 5;
        const BG_MIN:int =  -550;
        const BG_MAX:int = 0;
        const PBG_SPEED:int = 3;            

        var bg:BackGround = new BackGround; 
        var paraBg:ParaBg = new ParaBg;
        var toybox:TargetBox = new TargetBox;
        var toy:Toy = new Toy;
        var tryAgain:TryAgain = new TryAgain;
        var cheer:Cheer = new Cheer;
        var eightBit:EightBit = new EightBit;
        var countDown:Number = 30;
        var myTimer:Timer = new Timer(1000, 30);
        var myText:TextField = new TextField;
        var myText2:TextField = new TextField;
        var myTextFormat:TextFormat = new TextFormat;
        var myTextFormat2:TextFormat = new TextFormat;
        var font1:Font1 = new Font1;
        var kids:Kids = new Kids;
        var count:int = 0;
        var finalScore:int = 0;



        var score:Number = 0;

        var toy1:Toy1 = new Toy1;
        var toy2:Toy2 = new Toy2;
        var toy3:Toy3 = new Toy3;
        var toy4:Toy4 = new Toy4;
        var toy5:Toy5 = new Toy5;
        var toy6:Toy6 = new Toy6;
        var toy7:Toy7 = new Toy7;
        var toy8:Toy8 = new Toy8;
        var toy9:Toy9 = new Toy9;
        var toy10:Toy10 = new Toy10;
        var toy11:Toy11 = new Toy11;
        var toy12:Toy12 = new Toy12;
        var toy13:Toy13 = new Toy13;
        var toy14:Toy14 = new Toy14;
        var toy15:Toy15 = new Toy15;
        var toy16:Toy16 = new Toy16;
        var toy17:Toy17 = new Toy17;
        var toy18:Toy18 = new Toy18;
        var toy19:Toy19 = new Toy19;
        var toy20:Toy20 = new Toy20;

        var toyArray:Array = new Array(toy1, toy2, toy3, toy4, toy5, toy6, toy7, toy8, toy9, toy10, toy11, toy12, toy13, toy14, toy15, toy16, toy17, toy18, toy19, toy20);

    public function mainGame():void
    {
        trace("HI");
        eightBit.play(0, 9999);
        addChildAt(paraBg, 0);
        addChildAt(bg, 1);  
        addChildAt(kids, 2);
        kids.x = 310;
        kids.y = 200;
        addChild(toy);
        toy.x = 306;
        toy.y = 133;
        addChild(toybox);
        toybox.x = 295;
        toybox.y = 90;
    function addToys(xpos:int, ypos:int)
        {               
            addChild(toyArray[i]);
            toyArray[i].x = xpos;
            toyArray[i].y = ypos;   
        }

        for (var i:int = 0; i < toyArray.length; i++)
        {
            addToys(1140 * Math.random() + 20, 170 * Math.random() + 230);                  
        }
    }


        public function bgScroll (e:Event)
        {

            stage.addEventListener(MouseEvent.MOUSE_UP, arrayDrop);
            myTimer.addEventListener(TimerEvent.TIMER, countdown);
            myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
            myTimer.start();

            e.target.addEventListener(Event.ENTER_FRAME, collision);

            if (stage.mouseX > 600 && bg.x > BG_MIN)
            {
                bg.x -= BG_SPEED;   
                paraBg.x -= PBG_SPEED;

                for (var m:int=0; m< toyArray.length; m++)
                {
                (toyArray[m] as MovieClip).x -=BG_SPEED
                }

            }
            else if (stage.mouseX < 50 && bg.x < BG_MAX)
            {
                bg.x += BG_SPEED;
                paraBg.x += PBG_SPEED;
                for (var j:int=0; j< toyArray.length; j++)
                {
                (toyArray[j] as MovieClip).x +=BG_SPEED
                }                           
            }
            for (var k:int = 0; k < toyArray.length; k++)
            {
                toyArray[k].addEventListener(MouseEvent.MOUSE_DOWN, arrayGrab);


            }           

                bg.addEventListener(Event.ENTER_FRAME, bgScroll);       


        } // End of BGScroll

    public function collision (e:Event)
            {           
                for (var l:int=0; l< toyArray.length; l++)
                    {                       
                        if (toyArray[l].hitTestObject(toy))
                        {
                            removeChild(toyArray[l]);
                            toyArray[l].x=100000;
                            toybox.gotoAndPlay(2);
                            cheer.play(1, 1);
                            score = score + 10;
                            trace(score);

                        }   

                        if (score == 200)
                        {
                            timerDone();
                            myTimer.stop();                     
                        }
                    }
            }

    public function arrayGrab(e:MouseEvent)
            {
                e.target.startDrag();
            }

    public function arrayDrop(e:MouseEvent)
            {
                stopDrag();

            }

    public function resetGame(e:Event):void {
        trace("CLICK");
            countDown = 30;
            myText.text = "0" + countDown.toString();
            myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
            myTimer.reset();
            removeChild(tryAgain);
            myText2.visible = false;
            score = 0;
        }

    public function countdown(e:TimerEvent):void
        {           

            if (countDown > 0)
            {
                countDown--;
            }
            if (countDown < 10)
            {
                myText.text = "0" + countDown.toString();
                myText.x = 270;
                displayText();
            }
            else if (countDown < 20 && countDown > 9)
            {
                myText.text = countDown.toString();
                myText.x = 280;
                displayText();
            }
            else
            {
                myText.text = countDown.toString();
                myText.x = 270;
                displayText();
            }           

        } // end of countdown function

        public function displayText():void
            {   
                myText.filters = [new GlowFilter(0x00FF00, 1.0, 5, 5, 4)];
                addChild(myText);
                myText.width = 500, myText.height = 50, myText.y = 10;      
                myTextFormat.size = 50, myTextFormat.font = font1.fontName;                 
                myText.setTextFormat(myTextFormat);
            }

        public function displayText2():void
            {   myText2.filters = [new GlowFilter(0xFF0000, 1.0, 5, 5, 4)];
                addChild(myText2);
                myText2.width = 500, myText2.height = 35, myText2.x = 204, myText2.y = 200;     
                myTextFormat2.size = 30, myTextFormat2.font = font1.fontName;                   
                myText2.setTextFormat(myTextFormat2);
            }

            public function timerDone(e:TimerEvent=null):void
            {                           
                if (countDown == 0)
                {
                    count = 0;
                    finalScore = score;
                }
                else
                {                   
                    count = (30) - (myTimer.currentCount);
                    finalScore = (count * 10) + (score);

                }

                myText.text = "GAME OVER!";
                myText.x = 195;
                displayText();                          
                myText2.text = "Your score = " + (finalScore);
                displayText2();
                addChild(tryAgain);
                tryAgain.x = 300;
                tryAgain.y = 300;
                tryAgain.addEventListener(MouseEvent.CLICK, resetGame);
            }

} // End of class

} //包裹结束

1 个答案:

答案 0 :(得分:0)

没关系......解决了。 最简单的是改变

function addToys(xpos:int, ypos:int)
    {               
        addChild(toyArray[i]);
        toyArray[i].x = xpos;
        toyArray[i].y = ypos;   
    }

function addToys(xpos:int, ypos:int)
        {               
            stage.addChild(toyArray[i]);
            toyArray[i].x = xpos;
            toyArray[i].y = ypos;   
        }           

然后,在timerDone function I added this if语句中......

for (var m = 0; m < toyArray.length; m++) {
                    if (stage.contains(toyArray[m])) {
                    stage.removeChild(toyArray[m]);

                         }

做了一个款待!