Need serious help unloading movieclips in AS3.0

Hey guys I’m pretty new to actionscript 3.0 and thus I get the feeling I’m making a very juvenile mistake… either way I have been trying to solve this problem for hours to no avail. I have an assignment in uni to produce a basic flash game that is meant to meet a set criteria involving playing/pausing music, loading external swfs and so on which I have had no problem with.

The one issue I am having however is creating what is essentially a pond game in which a bird is meant to swoop down and pick up a fish. Getting a hit test to detect the collision between the fish and the bird is working fine but I want the fish to delete itself and then generate a new movieclip as the brief specifies. I can get it to generate a new movieclip no problem but for some reason I can’t get it to delete the original one.

I would greatly appreciate any help that you guys can provide. I understand that I may not be describing my problem very well and my code is pretty messy so I will attach both the fla and swf so you guys can see it in action.

I am not asking you to do my assignment for me, as you may have figure I have made every effort to figure this out on my own (as well as browsing this website) but I simply don’t know what to do :frowning:

Thanks guys. I have posted my entire code so you can get some insight into how the game would work as a whole, but the parts that are relevant to this problem are the functions on lines 90
(function fishArrayMaker(event:Event):void)
and 117
(function fishFunc(event:Event))
(specifically 156)
( //This is giving me problems event.target.removeChild(fish); )

I have attached the swf to this post as well (it may complain that it’s missing a couple external files but they are not necessary to reproduce the array problem) I have also uploaded the FLA version to rapidshare, it’s about 2mb so it’s too big to upload here.

The controls are quite simple, right arrow key to speed up, left to slow down and the down arrow to dive.

rapidsharedotcom/files/3570721008/coursework_2.zip

//Darasimi Makinde University of westminster Coursework Assignment 02
//Student number 12819403
//Fundamentals of computer animation EIGA401.2
stop();
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
import flash.events.Event;
import flash.display.Graphics;

// variables concerning the movement of the bird
var birdVelocity = 10;
var birdspeed = birdVelocity;
var birdDown = false;
var birdReturn = false;

var score = 0;

//external SWFs
var loader:Loader = new Loader();
var right:int = stage.stageWidth;
var left:int = 0;
var birdFlock:Array = new Array(bird5,bird4,bird3);

for (var i:Number=0; i<birdFlock.length; i++)
{
    var flockTween:Tween = new Tween(birdFlock*, "x", Regular.easeInOut, 
    birdFlock*.x, stage.stageWidth, 4, true);
    flockTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
}

function onFinish(e:TweenEvent):void
{
    e.target.obj.scaleX =  -  e.target.obj.scaleX;
    e.target.yoyo();
}
    
this.addChildAt(loader, 1);
var cloudsSWF:URLRequest=new URLRequest("cloud.swf");
loader.load(cloudsSWF);

/*this.addChildAt(loader, 2);
var birdsSWF:URLRequest=new URLRequest("extbird.swf");
loader.load(birdsSWF);*/

    

//direction for multiple fish
var fishDir:Number = Math.ceil(Math.random()*2)+3;
var fishDir1:Number = Math.ceil(Math.random()*2)+3;
/*var fishDir2:Number = Math.ceil(Math.random()*5)+3;
var fishDir3:Number = Math.ceil(Math.random()*5)+3;
var fishDir4:Number = Math.ceil(Math.random()*5)+3;*/
var fishes:Array = new Array(fish, fish1, fish2, fish3, fish4);

//fish starting positions
var startx = fish.x
var starty = fish.y
//var fishGroup:Array = new Array(fish,fish2,fish3);
//var birdOfPrey:MovieClip = null;
//var randnumtest:Number = Math.ceil(Math.random()*5)+5;

// timer
var timer:Timer = new Timer(1000, 60);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.start();
function countdown(event:TimerEvent) {
    time_txt.text = String(60 - timer.currentCount);
    if(timer.currentCount >= 60)
    {
        gotoAndStop("gameover");
        trace("time's up");
    }
    
//trace (timer.currentCount)
}

bird_mc.addEventListener(Event.ENTER_FRAME,birdFunc);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(Event.ENTER_FRAME, fishArrayMaker);
fish1.addEventListener(Event.ENTER_FRAME, fishFunc);
fish1.addEventListener(Event.ENTER_FRAME, fishFunc);
/*fish2.addEventListener(Event.ENTER_FRAME, fishFunc);
fish3.addEventListener(Event.ENTER_FRAME, fishFunc);
fish4.addEventListener(Event.ENTER_FRAME, fishFunc);
*/


//This handles the creation of new fishes
function fishArrayMaker(event:Event):void
{
velocity_txt.text = birdVelocity;
score_txt.text = score;


    for (var i:int = 0; i<fishes.length; i++)
    {
        var fish:MovieClip = fishes* as MovieClip;
        //parent.parent.removeChild(this)
        if (fish.hitTestObject(block1))
        {
            //fish.x -= 2
        }
        
/*    fish.x +=  fish.speed;
        if (fish.x > stage.stageWidth)
        {
            fish.parent.removeChild(fish);
            fishes.splice(i,1);
        }*/
    }

}


//existing fish arrays
function fishFunc(event:Event)
{
    {
        if (fish.hitTestObject(block1))
        {
            fishDir = -(Math.ceil(Math.random()*2)+3);
            //trace(fishDir)
            //fish.x -= fishDir
            }
            else if (fish.hitTestObject(block2))
            {
                fishDir = Math.ceil(Math.random()*2)+3;
                //trace(fishDir)
                }
                fish.x += fishDir
                }
        if (fish1.hitTestObject(block1))
        {
            fishDir1 = -2//(Math.ceil(Math.random()*5)+3);
            //trace(fishDir1)
        }
        else if (fish1.hitTestObject(block2))
        {
            fishDir1 = 2
            //trace(fishDir1)
        }
        fish1.x += fishDir1

    //fish.x += Math.ceil(Math.random()*5)+5;
    //trace (randnumtest)
    //if(fish.hitTestObject(bird_mc)||fish1.hitTestObject(bird_mc)||fish2.hitTestObject(bird_mc)||fish3.hitTestObject(bird_mc)||fish4.hitTestObject(bird_mc))
      if(event.target.hitTestObject(bird_mc))
      {
          {
              score++;
              var fish:fish_mc = new fish_mc()
              event.target.parent.addChild(fish)
              fishes.push(fish)
          }
          //This is giving me problems event.target.removeChild(fish);
           fish.x = 103.5
           fish.y = 471.25
           
           //fish.speed = Math.random() * 1 + .2
           //trace("hit detected")
       }
}

function keyDownHandler(event:KeyboardEvent ):void
{
    if (event.keyCode == 40 && birdDown == false)
    {
        //trace("down velocity " + birdVelocity);
        birdDown = true;
    }
    if (event.keyCode == 39 && birdDown == false)
    {
        birdVelocity +=  1;
        //trace(birdVelocity);
    }
    if (event.keyCode == 37 && birdDown == false)
    {
        birdVelocity -=  1;
        //trace(birdVelocity);
    }

}

/*function birdDown(event:Event)
{
bird_mc.y +=  20;
trace("bird down");
}*/
function birdFunc(event:Event)
{
    /*if (birdDown == false && birdReturn == false)
    {*/
    if (bird_mc.hitTestObject(block1))
    {
        birdspeed = birdVelocity;
        //birdVelocity =  -10;
        bird_mc.scaleX = 1
        bird_mc.gotoAndStop(1);
        //bird_mc.rotation = 0
        //bird_mc.scaleY = 1
    }
    else if (bird_mc.hitTestObject(block2))
    {
        birdspeed = -(birdVelocity);
        bird_mc.scaleX = -1
        bird_mc.rotation = 0
        bird_mc.scaleY = 1
    }

    if (birdDown == true)
    {
        if (bird_mc.hitTestObject(blockfloor))
        {
            bird_mc.y -=  birdVelocity;
            birdReturn = true;
            birdDown = false;
            //bird_mc.rotation = 360
            bird_mc.gotoAndStop(5);
        }
        else
        {
            bird_mc.y +=  birdVelocity;
            bird_mc.gotoAndStop(3);
            //bird_mc.rotation = 90
            //bird_mc.scaleY = -1
            //bird_mc.x +=  birdspeed;
        }
    }
    if (birdReturn == true)
    {

        bird_mc.y -=  birdVelocity;
        //bird_mc.x +=  0;
        if (bird_mc.hitTestObject(blockroof))
        {
            bird_mc.x +=  10
            birdVelocity = 10
            //trace ("new birdVelocity " + birdVelocity);
            birdReturn = false;
            bird_mc.gotoAndStop(1);
        }
    }
    bird_mc.x -= birdspeed;
}