# Need help in as3

**URL:** <https://forum.kirupa.com/t/need-help-in-as3/321691>\
**Category:** flash\
**Created:** [April 27, 2011, 5:45pm UTC](https://forum.kirupa.com/t/need-help-in-as3/321691 "2011-04-27T17:45:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kup0](https://avatars.discourse-cdn.com/v4/letter/k/b2d939/32.png) [@kup0](https://forum.kirupa.com/u/kup0)\
**Post date:** [April 27, 2011, 5:45pm UTC](https://forum.kirupa.com/t/need-help-in-as3/321691/1 "2011-04-27T17:45:36Z")

</div>

Hi, I’m currently trying to program a flash game using actionscript 3.0 where users would step on insects. Basically, I added multiple instances of insects movieclip and remove them by using hitTest function. When the mouse\_mc. hitTestObject(insect), there were times the insect was not removed.

Here are the errors i encountered:

Error #2025: The supplied DisplayObject must be a child of the caller.  
at flash.display::DisplayObjectContainer/swapChildren()  
at SteppingGame/hitTest()  
Error #1009: Cannot access a property or method of a null object reference.  
at insectmc/onEnterFrameEvent\_SF()

I have been cracking my head for the past few days and hope you guys could help me see what cause the problem.

Also, is there anyway i could simply my code like creating multiple arrays of movieclip insectmc?

Here’s my as3 code:  
[COLOR=Blue]package  
{  
import flash.display.MovieClip;  
import flash.events.MouseEvent;  
import flash.ui.Mouse;  
import flash.display.Sprite;  
import flash.text.TextFormat;  
import flash.text.TextFieldType;  
import flash.text.TextFieldAutoSize;  
import flash.utils.Timer;  
import flash.events.TimerEvent;  
import flash.events.Event;

```
public class SteppingGame extends MovieClip
{
    
    //No. of resets
    private var noReset:Number = 0;

    //init score equal to 0
    private var hitscore:Number = 0;

    //my insects
    private var insect1:insectmc;
    private var insect2:insectmc;
    private var insect3:insectmc;
    private var insect4:insectmc;
    private var insect5:insectmc;
    private var insect6:insectmc;
    private var insect7:insectmc;
    private var insect8:insectmc;
    private var insect9:insectmc;
    private var insect10:insectmc;

    //my fishes
    private var fish1:fishmc;
    private var fish2:fishmc;
    private var fish3:fishmc;

    private function startlevel1():void
    {
        
        showGameScore();
        stage.addEventListener(MouseEvent.CLICK, hitTest);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseListener);
        insect1= new insectmc();
        insect2= new insectmc();
        insect3= new insectmc();
        insect4= new insectmc();
        insect5= new insectmc();
        fish1 = new fishmc();
        fish2 = new fishmc();
        fish3 = new fishmc();
        addChild(fish1);
        addChild(fish2);
        addChild(fish3);
        addChild(insect1);
        addChild(insect2);
        addChild(insect3);
        addChild(insect4);
        addChild(insect5);
        trace(getChildAt(0));
        StartCountDown();
    }

    public function hitTest(event:MouseEvent)
    {
        trace(insect1);
        var squish:SquashMC = new SquashMC();
        if (mouse_mc.hitTestObject(insect1))
        {
            hitscore = hitscore + 10;
            squish.rotation = insect1.rotation;
            squish.x = mouse_mc.x;
            squish.y = mouse_mc.y;

            removeChild(insect1);
            addChildAt(squish,6);
        }
        else if (mouse_mc.hitTestObject(insect2))
        {
            hitscore = hitscore + 10;
            squish.rotation = insect2.rotation;
            removeChild(insect2);
            squish.x = mouse_mc.x;
            squish.y = mouse_mc.y;
            addChildAt(squish,6);
        }
        else if (mouse_mc.hitTestObject(insect3))
        {
            hitscore = hitscore + 10;
            squish.rotation = insect3.rotation;
            removeChild(insect3);
            squish.x = mouse_mc.x;
            squish.y = mouse_mc.y;
            addChildAt(squish,6);
        }
        else if (mouse_mc.hitTestObject(insect4))
        {
            hitscore = hitscore + 10;
            squish.rotation = insect4.rotation;
            removeChild(insect4);
            squish.x = mouse_mc.x;
            squish.y = mouse_mc.y;
            addChildAt(squish,6);
        }
        else if (mouse_mc.hitTestObject(insect5))
        {
            hitscore = hitscore + 10;
            squish.rotation = insect5.rotation;
            removeChild(insect5);
            squish.x = mouse_mc.x;
            squish.y = mouse_mc.y;
            addChildAt(squish,6);
        }
        else if (mouse_mc.hitTestObject(insect6))
        {
            hitscore = hitscore + 10;
            squish.rotation = insect6.rotation;
            removeChild(insect6);
            squish.x = mouse_mc.x;
            squish.y = mouse_mc.y;
            addChildAt(squish,6);
        }
        else if (mouse_mc.hitTestObject(insect7))
        {
            hitscore = hitscore + 10;
            squish.rotation = insect7.rotation;
            removeChild(insect7);
            squish.x = mouse_mc.x;
            squish.y = mouse_mc.y;
            addChildAt(squish,6);
        }
        else if (mouse_mc.hitTestObject(insect8))
        {
            hitscore = hitscore + 10;
            squish.rotation = insect8.rotation;
            removeChild(insect8);
            squish.x = mouse_mc.x;
            squish.y = mouse_mc.y;
            addChildAt(squish,6);
        }
        else if (mouse_mc.hitTestObject(insect9))
        {
            hitscore = hitscore + 10;
            squish.rotation = insect9.rotation;
            removeChild(insect9);
            squish.x = mouse_mc.x;
            squish.y = mouse_mc.y;
            addChildAt(squish,6);
        }
        else if (mouse_mc.hitTestObject(insect10))
        {
            hitscore = hitscore + 10;
            squish.rotation = insect10.rotation;
            removeChild(insect10);
            squish.x = mouse_mc.x;
            squish.y = mouse_mc.y;
            addChildAt(squish,6);
        }
        else
        {
            insect1.newpos();
            insect2.newpos();
            insect3.newpos();
            insect4.newpos();
            insect5.newpos();
            insect6.newpos();
            insect7.newpos();
            insect8.newpos();
            insect9.newpos();
            insect10.newpos();
        }
        showGameScore();

    }

```

[COLOR=Black]  
Pardon my messy codes, I’m quite new to as3.[/COLOR]  
[COLOR=Black]Thanks and Regards  
kup0[/COLOR][/COLOR]
