user3674001
user3674001

Reputation: 31

RemoveChild on Button Click

I'm currently trying to remove a graphic 'lose_mc' that i added to the scene using addChild.

When trying to use removeChild when the user clicks the next level_btn, this error appears:

TypeError: Error #2007: Parameter listener must be non-null.
    at flash.events::EventDispatcher/removeEventListener()
    at Function/<anonymous>()

Any one has any ideas of why this isn't working? This is my code at the minute. **

import flash.net.URLRequest;
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.Event;
import flash.display.Loader;
stop();
//stage.removeEventListener(Event.ENTER_FRAME, nextButtonClick);
var hitObstacle:Boolean=false;
var points=2; //The points will be set at 2
points_txt.text=points.toString(); //Health will be displayed in points_txt box
var lose_mc = new lose();
var win_mc = new win();
//crowhit_mc.visible=false;
var loader:Loader = new Loader()
addChild(loader);
//var url:URLRequest = new URLRequest("test.swf"); 
//var lose_mc = new lose();
//var win_mc = new win();
//var hitObstacle:Boolean=false;
var leafArray:Array = new Array(leaf01,leaf02,leaf03);
var leafsOnstage:Array = new Array();
var leafsCollected:int = 0;
var leafsLost:int = 0;
for (var i:int = 0; i<20; i++) {
    var pickLeaf = leafArray[int(Math.random() * leafArray.length)];
    var leaf:MovieClip = new pickLeaf();
    addChild(leaf);
    leaf.x = Math.random() * stage.stageWidth-leaf.width;// fruit.width is subtracted from the random x position to elimate the slight possibility that a clip will be placed offstage on the right.
    leaf.y = Math.random() * -500;
    leaf.speed = Math.random() * 15 + 5;
    leafsOnstage.push(leaf);
}
basket_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragBasket);
stage.addEventListener(MouseEvent.MOUSE_UP, dragStop);
function dragBasket(e:Event):void {
    basket_mc.startDrag();
}
function dragStop(e:Event):void {
    basket_mc.stopDrag();
}
stage.addEventListener(Event.ENTER_FRAME, catchLeaf);
function catchLeaf(e:Event):void {
    for (var i:int = leafsOnstage.length-1; i > -1; i--) {
        var currentLeaf:MovieClip = leafsOnstage[i];
        currentLeaf.y +=  currentLeaf.speed;
        if (currentLeaf.y > stage.stageHeight - currentLeaf.height) {
            currentLeaf.y = 0 - currentLeaf.height;
            leafsLost++;
            field2_txt.text = "Total Leaves Lost: " + leafsLost;
        }
        if (currentLeaf.hitTestObject(basket_mc)) {
            leafsCollected++;
            removeChild(currentLeaf);
            leafsOnstage.splice(i,1);


            field1_txt.text = "Total Leaves Collected: " + leafsCollected;
            if (leafsCollected >= 20) {
                basket_mc.gotoAndStop(20);
            } else if (leafsCollected > 15) {
                basket_mc.gotoAndStop(15);
            } else if (leafsCollected>10) {
                basket_mc.gotoAndStop(10);
            } else if (leafsCollected>5) {
                basket_mc.gotoAndStop(5);
            }
        }
    }
    if (leafsOnstage.length <= 0) {
        field1_txt.text = "You Win! You have collected enough leaves for the day.";

        nextlevel_btn.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolINwin);
        nextlevel_btn.alpha = 0;

        function fl_FadeSymbolINwin(event:Event)
        {
            nextlevel_btn.alpha += 0.01;
            if(nextlevel_btn.alpha >= 1)
    {
        nextlevel_btn.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolIn);
    }
        }

        field2_txt.text = "";
        stage.removeEventListener(Event.ENTER_FRAME, catchLeaf);


        var win_mc = new win();
        win_mc.x = 215.70;
        win_mc.y = 163.25;
        win_mc.w = 100;
        win_mc.h = 145;
        addChild(win_mc);
        removeChild(crow_mc);
    }

    if (leafsLost >= 20) {
        field1_txt.text = "Sorry you lose. You have lost too many leaves!";

restart_btn.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolIn);
restart_btn.alpha = 0;
function fl_FadeSymbolIn(event:Event)
{
    restart_btn.alpha += 0.01;
    if(restart_btn.alpha >= 1)

    {
        restart_btn.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolIn);
    }
}
        field2_txt.text = "";
        stage.removeEventListener(Event.ENTER_FRAME, catchLeaf);
        for (var j:int = leafsOnstage.length-1; j > -1; j--) {
            currentLeaf = leafsOnstage[j];
            removeChild(currentLeaf);
            leafsOnstage.splice(j,1);

            lose_mc.x = 215.70;
            lose_mc.y = 163.25;
            lose_mc.w = 100;
            lose_mc.h = 145;
            addChild(lose_mc); //this is the movieclip in my library im adding it to the screen

        }
    }
}


//nextlevel_btn.addEventListener(MouseEvent.CLICK, nextButtonClick);
/*restart_btn.addEventListener(MouseEvent.CLICK,  buttonClick)
function buttonClick(e:MouseEvent):void
{
     try{
loader_mc.unloadAndStop();
} catch(e:Error) {
}
      var urlRequest : URLRequest = new  URLRequest("test.swf");
      loader_mc.load(urlRequest);
      removeChild(win_mc);
}
    //function nextButtonClick(e:MouseEvent):void{
        //removeChild(win_mc);
        //loader.unloadAndStop();
        //var request:URLRequest = new URLRequest("test.swf");
        //loader.load(request);     
crow_mc.addEventListener(MouseEvent.CLICK,rotateCrow);
function rotateCrow(e:MouseEvent):void {
    crow_mc.rotation +=10;

    var myTween:Tween = new Tween(crow_mc, "alpha", Strong.easeOut, 1,10,25, true);


}
*/
    var crow_mcSpeedX:int = -6;
    var crow_mcSpeedY:int = 2;



stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(evt:Event) {


    crow_mc.x += crow_mcSpeedX;
    crow_mc.y += crow_mcSpeedY;
    //because the ball's position is measured by where its CENTER is...
    //...we need add or subtract half of its width or height to see if that SIDE is hitting a wall

    //first check the left and right boundaries
    if(crow_mc.x <= crow_mc.width/2){ //check if the x position of the left side of the ball is less than or equal to the left side of the screen, which would be 0
        crow_mc.x = crow_mc.width/2; //then set the ball's x position to that point, in case it already moved off the screen
        crow_mcSpeedX *= -1; //and multiply the ball's x speed by -1, which will make it move right instead of left

    } else if(crow_mc.x >= stage.stageWidth-crow_mc.width/2){ //check to see the right side of the ball is touching the right boundary, which would be 550
        crow_mc.x = stage.stageWidth-crow_mc.width/2; //reposition it, just in case
        crow_mcSpeedX *= -1; //multiply the x speed by -1 (now moving left, not right)

    }


    //now we do the same with the top and bottom of the screen
    if(crow_mc.y <= crow_mc.height/2){ //if the y position of the top of the ball is less than or equal to the top of the screen
        crow_mc.y = crow_mc.height/2; //like we did before, set it to that y position...
        crow_mcSpeedY *= -1; //...and reverse its y speed so that it is now going down instead of up

    } else if(crow_mc.y >= stage.stageHeight-crow_mc.height/2){ //if the bottom of the ball is lower than the bottom of the screen
        crow_mc.y = stage.stageHeight-crow_mc.height/2; //reposition it
        crow_mcSpeedY *= -1; //and reverse its y speed so that it is moving up now

    }
    //} 
    //points--; //Health is decreased by 1 each time a trap_mc is clicked
    //points_txt.text=points.toString(); 


 //lose_mc.visible=true;
 if (basket_mc.hitTestObject(crow_mc)) { //If the player_mc hits the sharkfin_mc
 if (hitObstacle==false){ //Only subtract health if hitObstacle is false
 trace("Hit Object")
//crow_mc.visible=false; //crow_mc becomes invisible
//removeChild(leaf);
//leafsOnstage.visable = false;
//lose_mc.x = 215.70;
//lose_mc.y = 163.25;
//lose_mc.w = 100;
//addChild(lose_mc);
points--; //Points are decreased by 1 each time crow_mc is hit - hittestobject()
points_txt.text=points.toString(); 
 }

 hitObstacle=true;
 }
    }


restart_btn.addEventListener(MouseEvent.CLICK, backButtonClick);
    function backButtonClick(e:MouseEvent):void{
        removeChild(lose_mc);
        loader.unloadAndStop();
        var request:URLRequest = new URLRequest("test.swf");
        loader.load(request);       
    }   

nextlevel_btn.addEventListener(MouseEvent.CLICK, nextButtonClick);
    function nextButtonClick(e:MouseEvent):void{
        loader.unloadAndStop();
        var request:URLRequest = new URLRequest("snowflake.swf");
        loader.load(request);       

    }

**

This is the area of code that is causing the problem..

**

  nextlevel_btn.addEventListener(MouseEvent.CLICK, nextButtonClick);

    function nextButtonClick(e:MouseEvent):void{
        removeChild(win_mc);
        loader.unloadAndStop();
        var request:URLRequest = new URLRequest("snowflake.swf");
        loader.load(request);       

    }   

**

Upvotes: 1

Views: 132

Answers (1)

Andrei Nikolaenko
Andrei Nikolaenko

Reputation: 1074

That is not the area causing the problem because Flash clearly says:

TypeError: Error #2007: Parameter listener must be non-null.
    at flash.events::EventDispatcher/removeEventListener()
    at Function/<anonymous>()

As we can see, the error is happening in removeEventListener.
removeEventListener takes two parameters: event and listener.
What we can also see, Flash says: parameter listener must be non-null.
That means that parameter listener passed to removeEventListener is null.
The part of your code that may cause the error is the following:

if (leafsOnstage.length <= 0) {
    field1_txt.text = "You Win! You have collected enough leaves for the day.";
    nextlevel_btn.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolINwin);
    nextlevel_btn.alpha = 0;

    function fl_FadeSymbolINwin(event:Event)
    {
        nextlevel_btn.alpha += 0.01;
        if(nextlevel_btn.alpha >= 1)
        {
            nextlevel_btn.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolIn);
        }
    }

First, you are adding listener fl_FadeSymbolINwin, but you remove a different listener fl_FadeSymbolIn. Second, for some reason you are declaring a function inside an if block. What you need to do is to get your code formatting right and check where and how you do removeEventListener's with what parameters. Trace them if needed.

Also I suggest using TweenLite (or even TweenNano) for fade effects. You won't have to mess with all these EventListeners. Your code for fading button would look as simple as this:

TweenNano.to(nextlevel_btn, 0.5, {alpha:1});

Upvotes: 3

Related Questions