hitTestObject问题//删除阶段错误#2025

时间:2013-08-16 18:22:36

标签: actionscript-3 flash

嘿:我到目前为止试图制作两件东西,但两件都不起作用:(

第一个是制作一排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但我做得很整齐!所以,如果有人帮助我,他会轻松跟踪代码

所以它是一个简单的FLA.fail,带有4个MC,在那个4 MC中没有代码 一颗子弹 一艘颜色为黄色的船 一个颜色为绿色的Ship2,所以我可以看到随机化 和一个类Turret的玩家

所以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);
            }
        }
    }

}   

}

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

提前致谢。

1 个答案:

答案 0 :(得分:0)

对不起我问这个问题的方式(通过回答),但它很想发表评论@mitim。 所以我已经完成了你所说的并追踪了错误,但它现在才有意义(或者它对我有用)并且稍微改变了代码的排列。但是它再次给了我一个错误,我甚至无法理解为什么当它(对象)在那一点上甚至不存在。 更改了doShips()的代码

//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
            if(shipArray[i].y  >120){ // stage.stageHeight)
                removeChild(shipArray[i]);
                shipArray.splice(i,1);
            }
            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]);
                    shipArray.splice(i,1);
                }
            }
        }
    }

当我控制移位 - 进入FLA并射击一些船只它给了我

TypeError: Error #1010: A term is undefined and has no properties.
    at MainClass/doShips()[/Users/STouch/Desktop/MyWork/BattleShips/TestShips/11111111 Perks22/ShipArray/MainClass.as:112]
    at MainClass/everyFrame()[/Users/STouch/Desktop/MyWork/BattleShips/TestShips/11111111 Perks22/ShipArray/MainClass.as:50]

那条线(112)是if (shipArray[i].hitTestObject(_bulletsArray[bcount])) {?!?! 为什么它会在此之前侦听我已经移除并拼接的元素(如果已经击中了船/盒,则将其拼接并从舞台上移除) 而奇怪的是,它每次都不会给我这个错误,只有当我拍摄2-3艘船并等待其余部分被移除时(超过舞台的120度)。就像我击中最后一艘船一样数组或第一个或类似的数组中有一个空白点。任何想法如何解决或现在做什么?