How Do I spawn Enemy at either the far left or the

2019-08-20 08:07发布

问题:

First I get This Error

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]

and here is my code

  // ******* 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);



            } 
        }
    }
}

I Understand that I need to reference the parent whenever I removeChild in the testCollision function but I dont know where.

Also I want the zombies to spawn out of the stage and move in towards the center at a smooth speed with the code I have they just seem to spawn sort of rearly and always to the left of the stage. So I would need to spawn them off the stage and have them move in to the center and change their ScaleX position to change their dirention but I dont know how to do that Please help.

回答1:

I think you can fix the error you listed by changing removeChild(tempLaser) to stage.removeChild(tempLaser) since the stage is where you added your bullets, so that's where you need to remove them from.

I'll give you a hint on the zombie movement, but you'll probably want to find a programming forum/friend/professor to help with general code design questions like this. In moveEnemies, you'll need to decide whether the zombie should move left/right (based on whether their x position is larger or smaller than the player's), and whether they should move up/down (based on whether their y position is larger or smaller than the player's).

For example, if their x position is larger than the player's, you would do tempEnemy.x -= tempEnemy.speed, and if smaller, you would do tempEnemy.x += tempEnemy.speed. But as I said, this site isn't really made for these types of design questions.