0

嘿:到目前为止,我正在尝试做两件事,但都没有工作:(

第一种是制作一排 5 艘船,将它们从上到左定位。然后添加一个事件,使它们向下移动。但是每次我尝试制作 5 艘新船的新行时……他们将自己定位在前一行的最后一艘船之后。我确实想到了一种方法来解决这个问题,即等待第一排船越过​​舞台然后将它们从舞台上移除并从阵列中拼接,但是......当我提高产卵速度时(当我有 2 行,舞台上有 5 艘船或更多时)这不起作用。

我认为第二个错误来自这个事实......我在舞台上同时有 2 行。

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MainClass/doShips()
at MainClass/everyFrame()

我不知道究竟要从代码中粘贴什么,所以我不得不将所有内容都推送到 FLA.fail 的 MainClass 中:D 但我做得很巧妙!所以如果有人帮助我,他会很容易地跟踪代码

所以它是一个带有 4 个 MC 的简单 FLA.fail 并且在这 4 个 MC 中没有代码 a Bullet a Ship 颜色为 Yellow 一个 Ship2 颜色为 Green 所以我可以看到随机化和具有 Turret 类的 Player

所以 FLA 的 MainClass 是:

package  {

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

public class MainClass extends MovieClip {

    protected var mouseSpeed:Number = 20;
    protected var _thePlayer:Turret = new Turret();
    protected var shipCount:Number;
    protected var shipCount2:Number;
    protected var shipArray:Array = new Array();
    protected var counter:int = 0;

    //variables for bullets
    private var shootCooldown:Number = 0; //speed of the fire 
    private var firing:Boolean = false;//bullets triger
    private var numToShoot:int = 1; //bullets
    const MAX_COOLDOWN = 10; //bullets timer
    protected var _bulletsArray:Array = new Array();


    public function MainClass() {
        _thePlayer.x = stage.stageWidth/2
        _thePlayer.y = stage.stageHeight - _thePlayer.height/2
        addChild(_thePlayer)
        addEventListener(Event.ENTER_FRAME, everyFrame)
        stage.addEventListener(MouseEvent.MOUSE_DOWN, startFire);
        stage.addEventListener(MouseEvent.MOUSE_UP, stopFire);
    }
    private function startFire(ev:MouseEvent) {
        firing = true;
    }
    private function stopFire(ev:MouseEvent) {
        firing = false;
    }

    //every frame do this
    protected function everyFrame(ev:Event):void{
        //Moving The Turret
        moveTheTurret();
        //increase the counter every frame
        counter++;
        if(counter % 70 == 0){
            //position the ships on the stage
            positionShips();
        }
        //handle the ships when they are added on the stage
        doShips();
        //when to fire
        updateFire();
        //handle the bullets
        doBullets();
    }

    //Moving The Turret
    protected function moveTheTurret() {
        if (mouseX > _thePlayer.x +15) {
            _thePlayer.x +=  mouseSpeed;
        } else if (mouseX < _thePlayer.x - 15) {
            _thePlayer.x -=  mouseSpeed;
        } else {
            _thePlayer.x = mouseX;
        }
    }


    //createShips and position them
    protected function positionShips() {
        shipCount = 3;
        shipCount2 = 2;
        var gap = 10;
        for (var i:int = 0; i < shipCount; i++) {
            var s = new Ship();
            shipArray.push(s);
        }
        for (var j:int = 0; j < shipCount2; j++) {
            s = new Ship2();
            shipArray.push(s);
        }
        var array:Array=new Array();
        while (shipArray.length>0) {
            var index:uint = Math.floor(Math.random() * shipArray.length);
            array.push(shipArray[index]);
            shipArray.splice(index,1);
        }
        shipArray = array;
        //shipsArray has been randomized
        for (var k:int = shipArray.length - 1; k >= 0; k--) {
            addChild(shipArray[k]);
            shipArray[k].x = shipArray[k].width/2 + (shipArray[k].width * k) + (gap*k);
        }
    }


    //move the ships
    protected function doShips() {
        for (var i:int = shipArray.length - 1; i >= 0; i--) {
            shipArray[i].y +=3 //make the Ships fall down

            for (var bcount= _bulletsArray.length-1; bcount >= 0; bcount--) {
                //if the bullet is touching the ship
                if (shipArray[i].hitTestObject(_bulletsArray[bcount])) {
                    //if we get here it means there`s is a collision
                    removeChild(_bulletsArray[bcount]);
                    _bulletsArray.splice(bcount,1);
                    removeChild(shipArray[i]);
                    _bulletsArray.splice(i,1);
                }
            }
            //if it gets over 380 remove from stage and splice from the array
            if(shipArray[i].y  > 380){ // stage.stageHeight)
                removeChild(shipArray[i]);
                shipArray.splice(i,1);
            }
        }
    }


    //when to fire
    public function updateFire() {
        //if we are currently holding the mouse down
        if(firing == true){
            fire();
        }
        //reduce the cooldown by 1 every frame
        shootCooldown--;
    }


    //Shoot bullets
    private function fire() {
        if (shootCooldown <= 0) {
            //reset the cooldown 
            shootCooldown = MAX_COOLDOWN
            for (var i=0; i<numToShoot; i++){
                //spown a bullet
                var b = new Bullet();
                //set the rotation of the bullet
                b.rotation = -90
                b.x = _thePlayer.x;
                b.y = _thePlayer.y;
                //add the bullet to the list/Array of _bulletsArray
                _bulletsArray.push(b);
                //add the bullet to the perent object;
                addChild(b);
            }
        }
    }


    //handling the bullets
    protected function doBullets(){
        //make a for loop to iterate all  the _bulletsArray on the screen
        for (var bcount:int = _bulletsArray.length-1; bcount>=0; bcount--) {
            //make the bullets move Up
            _bulletsArray[bcount].y -=20;
            //if the bullet is beyond the screen remove from the stage and the Array
            if(_bulletsArray[bcount].y < 0){
                removeChild(_bulletsArray[bcount])
                _bulletsArray.splice(bcount,1);
            }
        }
    }

}   

}

我不知道如何解决这两个问题。非常感谢您的帮助!!!

提前致谢。

4

0 回答 0