0

首先我得到这个错误

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at Game_fla::MainTimeline/testCollisions()[Game_fla.MainTimeline::frame1:205]

这是我的代码

  // ******* IMPORTS *****
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.events.KeyboardEvent;

//*****VARIABLES****

var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var shootDown:Boolean = false;

var ySpeed:int = 0;
var xSpeed:int = 0;

var scrollX:int = 0;
var scrollY:int = 0;

var speedConstant:int = 5;

var friction:Number = 0.6;

var level:Number = 1;

var bullets:Array = new Array();
var container_mc:MovieClip; 
var enemies:Array;
var tempEnemy:MovieClip;


// BUTTON EVENTS EITHER CLICKED OR NOT

left_btn.addEventListener(MouseEvent.MOUSE_DOWN, moveLeft);
right_btn.addEventListener(MouseEvent.MOUSE_DOWN, moveRight);
up_btn.addEventListener(MouseEvent.MOUSE_DOWN, moveUp);
shoot_btn.addEventListener(MouseEvent.MOUSE_DOWN, shootPressed);

left_btn.addEventListener(MouseEvent.MOUSE_UP, leftUp);
right_btn.addEventListener(MouseEvent.MOUSE_UP, rightUp);
up_btn.addEventListener(MouseEvent.MOUSE_UP, upUp);

stage.addEventListener(Event.ENTER_FRAME, makeEnemies);



player.gotoAndStop('still');


stage.addEventListener(Event.ENTER_FRAME,onenter);

function onenter(e:Event):void{
    if (rightPressed == true && leftPressed == false){
        player.x += 8;
        player.scaleX = 1;
        player.gotoAndStop("walking");
        cloud.x -= 8;


    } else if (leftPressed == true && rightPressed == false){
        player.x -= 8;
        player.scaleX = -1;
        player.gotoAndStop('walking');
        cloud.x += 8;

   } else if(upPressed == true && leftPressed == false && rightPressed == false){


       }
        else{  
        rightPressed = false;
        leftPressed = false;
        player.gotoAndStop('still')}

}
// **** MOVEMENT CONTROLS *********



function shootPressed(e:MouseEvent):void{
    shootDown = true;
    if(shootDown == true){
        fireBullet();
        }

    }


function fireBullet():void
{
    var playerDirection:String;
    if(player.scaleX < 0){
        playerDirection = "left";
    } else if(player.scaleX > 0){
        playerDirection = "right";
    }
    var bullet:Bullet = new Bullet(player.x, player.y, playerDirection);
    //bullets = new Array();
    bullet.y = player.y + 8;
    stage.addChild(bullet);
    bullets.push(bullet);
    trace(bullets);

}

// BUTTON FUNCTIONS

function moveLeft(e:MouseEvent):void
{
    if(MouseEvent.MOUSE_DOWN){
    leftPressed = true;
    }else if (MouseEvent.MOUSE_UP) {
        leftPressed = false;
        }

}
function moveRight(e:MouseEvent):void
{
    if (MouseEvent.MOUSE_DOWN){
    rightPressed = true;
    }else if (MouseEvent.MOUSE_UP){
        rightPressed = false;
        }

}
function moveUp(e:MouseEvent):void
{
    if(MouseEvent.MOUSE_DOWN){
        upPressed = true;
        } else if (MouseEvent.MOUSE_UP) {
            upPressed = false;
            }

}



function leftUp(e:MouseEvent):void
{
    leftPressed = false;
}
function rightUp(e:MouseEvent):void
{
    rightPressed = false;
}
function upUp(e:MouseEvent):void
{
    upPressed = false;
}




enemies = new Array();

//Call this function for how many enemies you want to make...
function makeEnemies(e:Event):void
{
    var chance:Number = Math.floor(Math.random() * 60);
if (chance <= 2){

    //Make sure a Library item linkage is set to Enemy...
    tempEnemy = new enemy();
    tempEnemy.speed = 80;
    tempEnemy.x = Math.round(Math.random() * stage.stageWidth) * -10;
    addChild(tempEnemy);
    enemies.push(tempEnemy);
    moveEnemies();
    }
}


function moveEnemies():void
{
    var tempEnemy:MovieClip;
    for (var i:int =enemies.length-1; i>=0; i--)
    {
        tempEnemy = enemies[i];
        tempEnemy.x += tempEnemy.speed;
        tempEnemy.y = 285;
    }
}

stage.addEventListener(Event.ENTER_FRAME, testCollisions);

//Check for collisions between an enemies array and a Lasers array
function testCollisions(e:Event):void
{

    var tempEnemy:MovieClip;
    var tempLaser:MovieClip;

    for (var i:int=enemies.length-1; i >= 0; i--)
    {
        tempEnemy = enemies[i];
        for (var j:int=bullets.length-1; j>=0; j--)
        {
            tempLaser = bullets[j];
            if (tempLaser.hitTestObject(tempEnemy))
            {
                removeChild(tempEnemy);
                removeChild(tempLaser);
                trace("BULLET HIT");
                stage.addEventListener(Event.ENTER_FRAME, testCollisions);



            } 
        }
    }
}

removeChild我知道每当我在函数中时我都需要引用父级,testCollision但我不知道在哪里。

此外,我希望僵尸从舞台中生成,并使用我拥有的代码以平稳的速度向中心移动,它们似乎只是在舞台的后方并且总是在舞台的左侧生成。所以我需要将它们从舞台上生成并让它们移动到中心并改变它们的 ScaleX 位置以改变它们的方向,但我不知道该怎么做请帮忙。

4

1 回答 1

0

removeChild(tempLaser)我认为您可以通过更改来修复您列出的错误,stage.removeChild(tempLaser)因为该阶段是您添加项目符号的位置,因此您需要从中删除它们。

我会给你一个关于僵尸运动的提示,但你可能想找一个编程论坛/朋友/教授来帮助解决像这样的一般代码设计问题。在moveEnemies中,您需要决定僵尸应该向左/向右移动(基于它们的 x 位置是否大于或小于玩家的位置),以及它们是否应该向上/向下移动(基于它们的 y 位置是更大还是小于玩家的)。

例如,如果他们的 x 位置大于玩家的,你会做tempEnemy.x -= tempEnemy.speed,如果更小,你会做tempEnemy.x += tempEnemy.speed。但正如我所说,这个网站并不是真正为这些类型的设计问题而设计的。

于 2013-03-23T05:21:50.373 回答