停止/删除所有内容然后更改场景

时间:2013-05-31 06:37:48

标签: actionscript-3

我正在制作一个射击游戏,当我死了它不会移除孩子它只是冻结他们在屏幕上。我希望能够停止所有操作,然后删除并更改屏幕。

    var gunLength:uint = 90;
    var bullets:Array = new Array();
    var bulletSpeed:uint = 20;
    var baddies:Array = new Array();
    var timer:Timer = new Timer(1000);
    timer.addEventListener(TimerEvent.TIMER, addBaddie);
    timer.start();
    var lives:Number = 0;

    stop();

    stage.addEventListener(MouseEvent.MOUSE_MOVE, aimGun);
    stage.addEventListener(MouseEvent.MOUSE_DOWN, fireGun);
    stage.addEventListener(Event.ENTER_FRAME, moveObjects);

    function addBaddie(evt:TimerEvent):void {
        var baddie:Baddie = new Baddie();
        var side:Number = Math.ceil(Math.random() * 4);
        if (side == 1) {
            baddie.x = Math.random() * stage.stageWidth;
            baddie.y = - baddie.height;
        } else if (side == 2) {
            baddie.x = stage.stageWidth + baddie.width;
            baddie.y = Math.random() * stage.stageHeight;
        } else if (side == 3) {
            baddie.x = Math.random() * stage.stageWidth;
            baddie.y = stage.stageHeight + baddie.height;
        } else if (side == 4) {
            baddie.x = - baddie.width
            baddie.y = Math.random() * stage.stageHeight;
        }
        baddie.angle = getAngle(baddie.x, baddie.y, gun.x, gun.y);
        baddie.speed = Math.ceil(Math.random() * 15);
        addChild(baddie);
        baddies.push(baddie);
    }

    function fireGun(evt:MouseEvent) {
        var bullet:Bullet = new Bullet();
        bullet.rotation = gun.rotation;
        bullet.x = gun.x + Math.cos(deg2rad(gun.rotation)) * gunLength;
        bullet.y = gun.y + Math.sin(deg2rad(gun.rotation)) * gunLength;
        addChild(bullet);
        bullets.push(bullet);
    }

    function moveObjects(evt:Event):void {
        moveBullets();
        moveBaddies();
    }

    function moveBullets():void {
        for (var i:int = 0; i < bullets.length; i++) {
            var dx = Math.cos(deg2rad(bullets[i].rotation)) * bulletSpeed;
            var dy = Math.sin(deg2rad(bullets[i].rotation)) * bulletSpeed;
            bullets[i].x += dx;
            bullets[i].y += dy;
        }
    }

    function moveBaddies():void {
        for (var i:int = 0; i < baddies.length; i++) {
            var dx = Math.cos(deg2rad(baddies[i].angle)) * baddies[i].speed;
            var dy = Math.sin(deg2rad(baddies[i].angle)) * baddies[i].speed;
            baddies[i].x += dx;
            baddies[i].y += dy;
            if (baddies[i].hitTestPoint(gun.x, gun.y, true)) {
                removeChild(baddies[i]);
                baddies.splice(i, 1);
                loseLife();
                lives -= 1;
                if(lives < 1){
                    gotoAndStop(1,"Dead");              
                    for each(var gun:Gun in gun){
                        removeChild(gun)
                    }

                    lives--;
                    trace("Lives = " + lives);
                }

            } else {
                checkForHit(baddies[i]);
            }
        }
    }

    function checkForHit(baddie:Baddie):void {
        for (var i:int = 0; i < bullets.length; i++) {
            if (baddie.hitTestPoint(bullets[i].x, bullets[i].y, true)) {
                removeChild(baddie);
                baddies.splice(baddies.indexOf(baddie), 1);

            }
        }
    }

    function loseLife():void {
    }

    function aimGun(evt:Event):void {
        gun.rotation = getAngle(gun.x, gun.y, mouseX, mouseY);
        var distance:Number = getDistance(gun.x, gun.y, mouseX, mouseY);
        var adjDistance:Number = distance / 12 - 7;
    }

    function getAngle(x1:Number, y1:Number, x2:Number, y2:Number):Number {
        var radians:Number = Math.atan2(y2 - y1, x2 - x1);
        return rad2deg(radians);
    }

    function getDistance(x1:Number, y1:Number, x2:Number, y2:Number):Number {
        var dx:Number = x2 - x1;
        var dy:Number = y2 - y1;
        return Math.sqrt(dx * dx + dy * dy);
    }

    function rad2deg(rad:Number):Number {
        return rad * (180 / Math.PI);
    }

    function deg2rad(deg:Number):Number {
        return deg * (Math.PI/180);
    }

    var target:MovieClip;

    initialiseCursor();

    function initialiseCursor():void {
        Mouse.hide();
        target = new Target();
        target.x = mouseX;
        target.y = mouseY;
        target.mouseEnabled = false;
        addChild(target);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, targetMove);
        stage.addEventListener(MouseEvent.MOUSE_DOWN, targetDown);
        stage.addEventListener(MouseEvent.MOUSE_UP, targetUp);
    }

    function targetMove(evt:MouseEvent):void {
        target.x = this.mouseX;
        target.y = this.mouseY;
    }


    function targetDown(evt:MouseEvent):void {
        target.gotoAndStop(2);
    }

    function targetUp(evt:MouseEvent):void {
        target.gotoAndStop(1);
    }

0 个答案:

没有答案