删除事件侦听器将无法正常工作。

时间:2014-09-29 12:01:42

标签: actionscript-3 flash

我的代码是这样的:

有一个创建笔记的功能。创建的注释要么好又坏。

有一个移动事件监听器可以使笔记移动。 有一个鼠标事件监听器,可以在单击时检查它是好还是坏。

两个侦听器中的remove事件侦听器都不起作用。 检查下面的//破坏评论。 (注释仍然移动,仍然可以点击)

已经尝试了很多东西,但没有运气。 :(

以前没有问题但是当我修改我的代码以便我可以将参数传递给听众时,它就会崩溃。所以我猜我做的方式有问题。

如果有人知道更好的方法来区分移动音符/点击的音符,无论它的好坏,都可以提供帮助!这就是我将参数传递给侦听器的原因。 :)

function SpawnNote(rpos:int):void
{
    rsn = int(Math.random() * 3) + 1;

    spawn = int(Math.random() * notes.length);
    var note:MovieClip = new notes[spawn]();
    addChild(note);

    rpos = int(Math.random() * 3) + 1;

    if (rpos==1)
    {
        note.x = pos1;
    }
    else if (rpos==2)
    {
        note.x = pos2;
    }
    else if (rpos==3)
    {
        note.x = pos3;
    }

    note.y = 150;
    note.addEventListener(Event.ENTER_FRAME, MoveNote(spawn));
    note.addEventListener(MouseEvent.CLICK, CheckNote(spawn));

if(rsn%2==0)
{   

    rtemp = rpos;

    if (rtemp==1)
    {
        rpos = int(Math.random() * 2) + 2;
    }

    if (rtemp==2)
    {
        rpos = int(Math.random() * 2);
        if (rpos==0)
        {
            rpos = 3;
        }
    }

    if (rtemp==3)
    {
        rpos = int(Math.random() * 2) + 1;
    }

    spawn = int(Math.random() * notes.length);
    var note:MovieClip = new notes[spawn]();
    addChild(note);

    if (rpos==1)
    {
        note.x = pos1;
    }
    else if (rpos==2)
    {
        note.x = pos2;
    }
    else if (rpos==3)
    {
        note.x = pos3;
    }

    note.y = 150;

    note.addEventListener(Event.ENTER_FRAME, MoveNote(spawn));
    note.addEventListener(MouseEvent.CLICK, CheckNote(spawn));
}   


function MoveNote(spawncheck:int):Function
{
    return function(event:Event):void {
    var note:DisplayObject = event.target as DisplayObject;

    note.y +=  7;           

    if (note.y >= stage.stageHeight + 50)
    {
        if(spawncheck<15)
        {
            trace(spawn+" GOOD ITEM PASSED!");              
            score+=100; 
        }

        else
        {
            trace(spawn+" BAD ITEM PASSED!");
            score-=100;             
        }
        //if (note.parent)
        //{
            //REMOVE EVENT LISTENER BROKEN
            //e.currentTarget.removeEventListener(Event.type, MoveNote(spawn));
            //e.currentTarget.removeEventListener(Event.type, CheckNote(spawn));
        note.removeEventListener(Event.ENTER_FRAME, MoveNote);
        note.removeEventListener(MouseEvent.CLICK, CheckNote);
        note.parent.removeChild(note); 
        //removeChild(note); 
        //}                     
        scorecon.text = score.toString();       
    }
    }
}


function CheckNote(spawncheck:int):Function
{
    return function(e:MouseEvent):void {
    var note:DisplayObject = e.target as DisplayObject;

    if(spawncheck<15)
    {
        trace("GOOD ITEM!");
        score-=100;
    }

    else
    {
        trace("BAD ITEM!");
        score+=100; 
    }   
    scorecon.text = score.toString();           

    //BROKEN
    note.removeEventListener(Event.ENTER_FRAME, MoveNote);
    note.removeEventListener(MouseEvent.CLICK, CheckNote);
    note.x = stage.stageHeight/2;
    note.y = stage.stageWidth/2; 
    /*

    if (clicked.parent)
    {       
        clicked.parent.removeChild(clicked);
    }
    */
    //removeChild(note);
    }
}
}

2 个答案:

答案 0 :(得分:0)

你不能写

note.addEventListener(Event.ENTER_FRAME, MoveNote(spawn));
note.addEventListener(MouseEvent.CLICK, CheckNote(spawn));

正确的语法是

note.addEventListener(Event.ENTER_FRAME, MoveNote);
note.addEventListener(MouseEvent.CLICK, CheckNote);

虽然监听器功能应该如下所示

function MoveNote(e:Event):void{
...
}

function CheckNote(e:MouseEvent):void{
...
}

您不能将带参数的函数调用作为另一个函数调用的参数传递。 .addEventListener只接受对函数的引用。 你也不应该在你的监听器函数中返回Function,只需使用void并忘记返回任何东西。

您需要找出一种不同的方法来检查“spawncheck:int”对象。 这里有一个建议:重写你的代码,使“Notes”不是Movieclips,而是来自Class Note的对象。为类提供一个公共变量spawnCheck,并在EventListener函数中检查它。

一般来说,你应该重新学习AS3编码的基础知识。

答案 1 :(得分:0)

如前所述,您的变量范围是错误的。声明函数的var note OUTSIDE,然后你将删除正确变量的监听器:

  var note:MovieClip;

  function SpawnNote(rpos:int):void
  {
     rsn = int(Math.random() * 3) + 1;

     spawn = int(Math.random() * notes.length);
     note = new notes[spawn]();
     note.spawn = spawn; //so you don't need to pass the parameter and get the value later but I would use an array or dictionary to track them
     ....
相关问题