2015-03-19 3 views
1

В настоящее время я пытаюсь удалить графический «lost_mc», который я добавил в сцену с помощью addChild.RemoveChild on Button Нажмите

При попытке использовать RemoveChild, когда пользователь нажимает на следующий level_btn, появляется эта ошибка:

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

Любой из них имеет какие-либо идеи о том, почему это не работает? Это мой код в минуту. **

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

    } 

**

Это область кода, который вызывает проблему ..

**

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

    } 

**

+0

Это довольно немного кода. Можете ли вы сузить до того места, где вы думаете, проблема? thnx – Coffee

+1

Простите, да, это больше, чем я изначально думал .. это область кода, которая, кажется, проблема. Кнопка RemoveChild с кнопки перезапуска работает исправно, кажется, что removeChild от кнопки nextLevel, которая вызывает проблему. – user3674001

ответ

3

Это не область, вызывающая проблему, потому что Flash четко говорит:

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

Как видим, ошибка происходит в removeEventListener.
removeEventListener имеет два параметра: event и listener.
Что мы можем видеть, Flash говорит: параметр listener должен быть не нулевым.
Это означает, что параметр listener принят к removeEventListener null.
Часть кода, который может привести к ошибке является следующее:

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); 
     } 
    } 

Во-первых, вы добавляете слушатель fl_FadeSymbolINwin, но вы удалите другой слушатель fl_FadeSymbolIn. Во-вторых, по какой-то причине вы объявляете функцию внутри блока if. Что вам нужно сделать, это правильно настроить форматирование кода и проверить, где и как вы делаете removeEventListener с какими параметрами. Проследите их, если необходимо.

Также предлагаю использовать TweenLite (или даже TweenNano) для эффектов затухания. Вам не придется возиться со всеми этими EventListeners. Ваш код для кнопки угасания будет выглядеть так просто:

TweenNano.to(nextlevel_btn, 0.5, {alpha:1}); 
Смежные вопросы