removing event listener

前端 未结 3 608
终归单人心
终归单人心 2021-01-27 06:09

I have learned this method of passing values with addEventLister. Here is the code:

for (var i:uint = 0; i < asteroids.length; i++)
{

     asteroids[i].x = M         


        
3条回答
  •  Happy的楠姐
    2021-01-27 06:39

    Why don't you declare the function like normal, rather than in the parameter? You can do the following:

    for (var i:uint = 0; i < asteroids.length; i++)
    {
    
         asteroids[i].x = Math.random() * 450;
         asteroids[i].y = Math.random() * 450;
         asteroids[i].addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
         });
    
    }
    
    public function onMouseUp(e:MouseEvent) {
        changeValue(e, otherArguments);
    }
    
    public function changeValue(event:MouseEvent, otherArguments:Object):void
    {
        playSound(anote);
        trace(event.currentTarget);
    
    }
    

    You can remove it as follows:

    asteroids[index].removeEventListener(MouseEvent.MOUSE_UP, onMouseUp)
    

提交回复
热议问题