0

I am making a missile interception strategy game, why does it give an error message when trying to removing enemy birds?

  • How can the motion of the rocket be optimized ?
  • Why aren't the enemies eliminated?

Here is my Build Rocket code...

stage.addEventListener(MouseEvent.CLICK, letsgoeasy)
function letsgoeasy(e: MouseEvent): void {
    var Rocket: Hawk = new Hawk();
    Rocket.x = p.x;
    Rocket.y = p.y;
    Rocket.scaleX = 0.2;
    Rocket.scaleY = 0.2;
    addChild(Rocket);

    Rocket.addEventListener(Event.ENTER_FRAME, moveBullet);
    function moveBullet(e: Event) {

    }
    var myTimer: Timer = new Timer(2000);
    var speedF: Number = 5;
    var RocketBox: Array = new Array;
    Rocket.addEventListener(Event.ENTER_FRAME, follow);
    function follow(e: Event): void {
        myTimer.start();
        if (enemyFleet[h].y > 100) {
            for (var h: int = 0; h < enemyFleet.length; h++) {
                Rocket.x -= (Rocket.x - enemyFleet[h].x) / speedF;
                Rocket.x = Rocket.x + 2;
                Rocket.y -= (Rocket.y - enemyFleet[h].y) / speedF;
                Rocket.y = Rocket.y + 3;
                RocketBox.push(Rocket)
            }
        }
//Fire Rocket part
        var smoke: smoke_shell = new smoke_shell();
        addChild(smoke);
        smoke.scaleX = 0.1;
        smoke.scaleY = 0.1;
        smoke.x = Rocket.x
        smoke.y = Rocket.y + 10
        smoke.rotation = 0
        smoke.color = 0xE77471;
        smoke.blurX = 3;
        smoke.blurY = 3;
        smoke.strength = 100;
        smoke.quality = 3;
        removeChild(null);
    }
  // collision part
addEventListener(Event.ENTER_FRAME, collCkeck);
    function collCkeck(e: Event): void {
        for (var s: int = Rocket.length - 1; s >= 0; s--) {
            if (Rocket.hitTestObject(enemyFleet[s])) {
                enemyDeaths();
            }
            function enemyDeaths() {
                removeChild(Rocket);
                trace("Rocket");
                removeChild(enemyFleet[s]);
                enemyFleet.splice(s, 1);
            }
        }
    }
}

My error:

TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/removeChild() at Function/TrackermissileRocket190714002_fla:MainTimeline/letsgoeasy/TrackermissileRocket190714002_fla:follow()[TrackermissileRocket190714002_fla.MainTimeline::frame1:101]

My game image:

VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Without going any deeper, this: **removeChild(null);** – Organis Oct 12 '21 at 19:00
  • First, please conduct a proper search prior to posting. A simple googling will give you the idea, what **Error #1010** is about. Second, if you really need help, please think twice (or more), if it is even possible to help you. How could anyone guess, which one line of the code above is line 78? Also, consider *editing* your question to provide the MCVE: https://stackoverflow.com/help/minimal-reproducible-example – Organis Oct 12 '21 at 19:40
  • **Not clear**... (1) We can't see what is "not optimized" about your motion so tell us what is current result and what is the actual expected result? **(2)** Why `myTimer.start();` and also why start a timer inside an EnterFrame event since I assume the timer is forced to (re-)start every frame of your game's FPS (so running at 30 FPS means you get 30 timer re-starts per each second) – VC.One Oct 13 '21 at 09:58
  • PS: **(3)** About removing Enemies, check your `var s` in the FOR loop. You say `...s >= 0; s--` so if **S** is bigger than or **equal to** zero then minus it, at some point if **S** is zero and is also minused then it becomes `-1` which does not exist in Arrays... Maybe have a starting line to check `if( s < 0 ) { return; }` before doing the other IF as `if (Rocket.hitTestObject....` – VC.One Oct 13 '21 at 10:05
  • Hi, @VC.One I need a simple command to launch a rocket for the game that can track close targets after a few seconds. – Bayat Bros Oct 13 '21 at 20:28
  • PPS... **(4)** Try as: `if( (Rocket.hitTestObject(enemyFleet[s])) == true) { enemyDeaths(); }` for removing enemies. – VC.One Oct 13 '21 at 20:30
  • @VC.One TypeError: Error #2007: Parameter hitTestObject must be non-null. at flash.display::DisplayObject/_hitTest() at flash.display::DisplayObject/hitTestObject() – Bayat Bros Oct 13 '21 at 20:39
  • I think this is the problem: `var s: int = Rocket.length` because Rocket is an MC not an Array, so maybe you wanted `RocketBox`?? Try as: `var s: int = RocketBox.length`... – VC.One Oct 13 '21 at 21:06
  • PS: **(1)** You should make your vars as **global** (declare outside of _and_ before the functions part). **(2)** Don't trap vars and functions inside other functions... – VC.One Oct 13 '21 at 22:59

1 Answers1

0
  • (1) You should make your vars as global (outside of and best before the functions part).

  • (2) Don't trap vars and functions inside other functions...

Try a setup similar to something like this:

//# globally declare the VARS (now available to all functions)...
var Rocket: Hawk;
var myTimer: Timer;
var RocketBox : Array = new Array;
var speedF: Number = 0;

stage.addEventListener(MouseEvent.CLICK, letsgoeasy);

function letsgoeasy(e: MouseEvent): void 
{
    speedF = 5;
    
    Rocket = new Hawk();
    Rocket.x = p.x;
    Rocket.y = p.y;
    Rocket.scaleX = Rocket.scaleY = 0.2;

    Rocket.addEventListener(Event.ENTER_FRAME, moveBullet);
    Rocket.addEventListener(Event.ENTER_FRAME, follow);
    Rocket.addEventListener(Event.ENTER_FRAME, collCkeck);
    
    addChild(Rocket);
    RocketBox.push( Rocket );
    
    myTimer = new Timer(2000); //# what for...?
    myTimer.start(); //# maybe start Timer here at creation...?
}
 
        
function moveBullet(e: Event) : void 
{
        
}

 
function follow( currRocket : Event) : void 
{
    //myTimer.start();
    
    for (var h: int = 0; h < (enemyFleet.length-1); h++) 
    {
        if (enemyFleet[h].y > 100) 
        {
            currRocket.x -= (currRocket.x - enemyFleet[h].x) / speedF;
            currRocket.x = currRocket.x + 2;
            currRocket.y -= (currRocket.y - enemyFleet[h].y) / speedF;
            currRocket.y = currRocket.y + 3;
            //RocketBox.push( currRocket );
        }
    }
}


/////////


function collCkeck( currRocket : Event ) : void 
{
    for (var s: int = (RocketBox.length-1); s >= 0; s--) 
    {
        if ( (s >= 0 ) && ( ( currRocket.hitTestObject(enemyFleet[s])) == true ) )
        {
            enemyDeaths( s );
        }   
    }
}


function enemyDeaths( input_S ) : void 
{
    removeChild(Rocket);
    trace("Removed... Rocket");
    removeChild(enemyFleet[input_S]);
    enemyFleet.splice(input_S, 1);
}
VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Thanks, the 'myTimer' reason for interrupting a rocket launch is to create a curve in the path so that it does not hit the target directly. – Bayat Bros Oct 14 '21 at 05:11