如何在actionscript 3中更改嵌套类

时间:2015-11-08 06:02:59

标签: actionscript-3 flash nested

我正在处理一个动作脚本3代码,该代码从其他动作脚本3中提取信息。主要代码是Herbfight,它引用了其他4个(子弹,错误,Goodbugs和Sprayer)。下面我试图将它们全部合并为一个动作脚本。这样做的原因是我想把它作为符号拉入而不是让flash文件与类相关。 问题是它说我不能拥有嵌套类。关于我如何解决这个问题的任何想法?下面是我尝试组合代码。 谢谢, Ĵ     包{     import flash.display。;     import flash.events。;     import flash.utils.Timer;     import flash.text.TextField;     import flash.text。*;     import flash.utils.getTimer;     import flash.geom.Point;

public class herbfight_v004 extends MovieClip {
    private var aagun:Sprayer;
    private var airplanes:Array;
    private var goodbug:Array;
    private var bullets:Array;
    private var upArrow, downArrow:Boolean;
    private var nextPlane:Timer;
    private var nextGbugs:Timer;
    private var shotsLeft:int;
    private var shotsHit:int;

    private var Sprayer:MovieClip
    private var Bullets:MovieClip
    private var bugs:MovieClip
    private var GooodBugs:MovieClip



    public function startherbfight_v004() {

        // init score
        shotsLeft = 20;
        shotsHit = 0;
        showGameScore();

        // create gun
        aagun = new Sprayer();
        addChild(aagun);

        // create object arrays
        airplanes = new Array();
        bullets = new Array();
        goodbug = new Array();

        // listen for keyboard
        stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
        stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction);

        // look for collisions
        addEventListener(Event.ENTER_FRAME,checkForHits);

        // start planes flying
        setNextPlane();
        setNextGbugs();






    public function Sprayer() { 
 {
    // animation time// initial location of gun
        this.x = 410;
        this.y = 380;

        // movement
        addEventListener(Event.ENTER_FRAME,moveGun);
    }

    public function moveGun(event:Event) {
        // get time difference
        var timePassed:int = getTimer()-lastTime;
        lastTime += timePassed;

        // current position
        var newy = this.y;

        // move to the left
        if (MovieClip(parent).upArrow) {
            newy -= speed*timePassed/1000;
        }

        // move to the right
        if (MovieClip(parent).downArrow) {
            newy += speed*timePassed/1000;
        }

        // check boundaries
        if (newy < 65) newy = 65;
        if (newy > 380) newy = 380;

        // reposition
        this.y = newy;
    }

    // remove from screen and remove events
    public function deleteGun() {
        parent.removeChild(this);
        removeEventListener(Event.ENTER_FRAME,moveGun);
    }
}








    public function bugs(side:String, speed:Number, altitude:Number) {
{                   
        if (side == "left") {
            this.x = -50; // start to the left
            dx = speed; // fly left to right
            this.scaleX = 1; // reverse
        } else if (side == "right") {
            this.x = -50; // start to the right
            dx = -speed; // fly right to left
            this.scaleX = 1; // not reverse
        }
        this.y = altitude; // vertical position

        // choose a random plane
        this.gotoAndStop(Math.floor(Math.random()*4+1));

        // set up animation
        addEventListener(Event.ENTER_FRAME,movePlane);
        lastTime = getTimer();
    }

    public function movePlane(event:Event) {
        // get time passed
        var timePassed:int = getTimer()-lastTime;
        lastTime += timePassed;

        // move plane
        this.x += dx*timePassed/2000;

        // check to see if off screen
        if ((dx < 0) && (x < -50)) {
            deletePlane();
        } else if ((dx > 0) && (x > 350)) {
            deletePlane();
        }

    }

    // plane hit, show explosion
    public function planeHit() {
        removeEventListener(Event.ENTER_FRAME,movePlane);
        MovieClip(parent).removePlane(this);
        gotoAndPlay("explode");
    }

    // delete plane from stage and plane list
    public function deletePlane() {
        removeEventListener(Event.ENTER_FRAME,movePlane);
        MovieClip(parent).removePlane(this);
        parent.removeChild(this);
    }
}




    public function Bullets(x,y:Number, speed: Number) {
{
        // set start position
        this.x = x;
        this.y = y;
        // get speed
        dx = speed;
        // set up animation
        lastTime = getTimer();
        addEventListener(Event.ENTER_FRAME,moveBullet);
    }

    public function moveBullet(event:Event) {
        // get time passed
        var timePassed:int = getTimer()-lastTime;
        lastTime += timePassed;

        // move bullet
        this.x += dx*timePassed/1000;

        // bullet past top of screen
        if (this.x < 0) {
            deleteBullet();
        }

    }

    // delete bullet from stage and plane list
    public function deleteBullet() {
        MovieClip(parent).removeBullet(this);
        parent.removeChild(this);
        removeEventListener(Event.ENTER_FRAME,moveBullet);
    }
}




    public function GoodBugs(side:String, speed:Number, altitude:Number) {
{
        if (side == "left") {
            this.x = -50; // start to the left
            dx = speed; // fly left to right
            this.scaleX = -1; // reverse
        } else if (side == "right") {
            this.x = -50; // start to the right
            dx = -speed; // fly right to left
            this.scaleX = -1; // not reverse
        }
        this.y = altitude; // vertical position

        // choose a random Gbugs
        this.gotoAndStop(Math.floor(Math.random()*2+1));

        // set up animation
        addEventListener(Event.ENTER_FRAME,moveGbugs);
        lastTime = getTimer();
    }

    public function moveGbugs(event:Event) {
        // get time passed
        var timePassed:int = getTimer()-lastTime;
        lastTime += timePassed;

        // move Gbugs
        this.x += dx*timePassed/2000;

        // check to see if off screen
        if ((dx < 0) && (x < -50)) {
            deleteGbugs();
        } else if ((dx > 0) && (x > 350)) {
            deleteGbugs();
        }

    }

    // Gbugs hit, show explosion
    public function GbugsHit() {
        removeEventListener(Event.ENTER_FRAME,moveGbugs);
        MovieClip(parent).removeGbugs(this);
        gotoAndPlay("explode");
    }

    // delete Gbugs from stage and Gbugs list
    public function deleteGbugs() {
        removeEventListener(Event.ENTER_FRAME,moveGbugs);
        MovieClip(parent).removeGbugs(this);
        parent.removeChild(this);
    }

}




    public function setNextPlane() {
        nextPlane = new Timer(1000+Math.random()*1000,1);
        nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
        nextPlane.start();
    }

    public function newPlane(event:TimerEvent) {
        // random side, speed and altitude
        if (Math.random() > .2) {
            var side:String = "left";
        } else {
            side = "right";
        }
        var altitude:Number = Math.random()*280+80;
        var speed:Number = Math.random()*150+150;

        // create plane
        var p:bugs = new bugs(side,speed,altitude);
        addChild(p);
        airplanes.push(p);

        // set time for next plane
        setNextPlane();
    }



    public function setNextGbugs() {
        nextGbugs = new Timer(1000+Math.random()*1000,1);
        nextGbugs.addEventListener(TimerEvent.TIMER_COMPLETE,newGbugs);
        nextGbugs.start();
    }

    public function newGbugs(event:TimerEvent) {
        // random side, speed and altitude
        if (Math.random() > .2) {
            var side:String = "left";
        } else {
            side = "right";
        }
        var altitude:Number = Math.random()*280+80;
        var speed:Number = Math.random()*150+150;

        // create Gbugs
        var p:GoodBugs = new GoodBugs(side,speed,altitude);
        addChild(p);
        goodbug.push(p);

        // set time for next Gbugs
        setNextGbugs();
    }

    // check for collisions
    public function checkForHits(event:Event) {
        for(var bulletNum:int=bullets.length-1;bulletNum>=0;bulletNum--){ 
            for (var airplaneNum:int=airplanes.length-1;airplaneNum>=0;airplaneNum--) {
                if (bullets[bulletNum].hitTestObject(airplanes[airplaneNum])) {
                    airplanes[airplaneNum].planeHit();
                    bullets[bulletNum].deleteBullet();
                    shotsHit++;
                    showGameScore();
                    break;
                }
            }
        }

        for(var bulletNum:int=bullets.length-1;bulletNum>=0;bulletNum--){ 
            for (var GoodBugsNum:int= goodbug.length-1; GoodBugsNum>=0; GoodBugsNum--) {
                if (bullets[bulletNum].hitTestObject(goodbug [GoodBugsNum])) {
                    goodbug [GoodBugsNum]. GbugsHit();
                    bullets[bulletNum].deleteBullet();
                    shotsHit--;
                    showGameScore();
                    break;
                }
            }
        }


        if ((shotsLeft == 0) && (bullets.length == 0)) {
            endGame();
        }
    }

    // key pressed
    public function keyDownFunction(event:KeyboardEvent) {
        if (event.keyCode == 38) {
            upArrow = true;
        } else if (event.keyCode == 40) {
            downArrow = true;
        } else if (event.keyCode == 32) {
            fireBullet();
        }
    }

    // key lifted
    public function keyUpFunction(event:KeyboardEvent) {
        if (event.keyCode == 38) {
            upArrow = false;
        } else if (event.keyCode == 40) {
            downArrow = false;
        }
    }

    // new bullet created
    public function fireBullet() {
        if (shotsLeft <= 0) return;
        var b:Bullets = new Bullets(aagun.x,aagun.y,-300);
        addChild(b);
        bullets.push(b);
        shotsLeft--;
        showGameScore();
    }

    public function showGameScore() {
        showScore.text = String("Score: "+shotsHit);
        showShots.text = String("Shots Left: "+shotsLeft);
    }

    // take a plane from the array
    public function removePlane(plane:bugs) {
        for(var i in airplanes) {
            if (airplanes[i] == plane) {
                airplanes.splice(i,1);
                break;
            }
        }
    }
    // take a plane from the array
    public function removeGbugs(Gbugs:GoodBugs) {
        for(var i in goodbug) {
            if (goodbug[i] == Gbugs) {
                goodbug.splice(i,1);
                break;
            }
        }
    }

    // take a bullet from the array
    public function removeBullet(bullet:Bullets) {
        for(var i in bullets) {
            if (bullets[i] == bullet) {
                bullets.splice(i,1);
                break;
            }
        }
    }

    // game is over, clear movie clips
    public function endGame() {
        // remove planes
        for(var i:int=airplanes.length-1;i>=0;i--) {
            airplanes[i].deletePlane();
        }
for(var i:int= goodbug.length-1;i>=0;i--) {
            goodbug [i].deleteGbugs();
        }

        airplanes = null;
        goodbug = null
        aagun.deleteGun();
        aagun = null;

        stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
        stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
        removeEventListener(Event.ENTER_FRAME,checkForHits);

        nextPlane.stop();
        nextPlane = null;

        nextGbugs.stop();
        nextGbugs = null;


        gotoAndStop("gameover");
    }

}
}

0 个答案:

没有答案