1
votes

My code goes like this:

There is a function that creates notes. The notes created are either good or bad.

There is a move event listener that makes the notes move. There is a mouse event listener that check the note if it's good or bad when clicked.

The remove event listener in both listener does not work. Check the //broken comment below. (notes still move, and can still be clicked)

Already tried a lot of things, but no luck. :(

There's no problem before but when I modified my code so I can pass parameters in to the listeners, it broken down. So I'm guessing there is something wrong with the way I did it.

If anyone knows a better way to differentiate the moving note/clicked note if it's good or bad, do help! That's the reason why I'm passing parameters into the listeners by the way. :)

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
Those listeners have no scope therefore you can't reference them and can't remove them. You can only use weakListener flag with that. But the whole code style is weak and unprofessional anyway. Going to a great length of code crap, workarounds and hacks in order to avoid writing a simple custom event class.BotMaster

2 Answers

0
votes

You cannot write

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

The correct syntax would be

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

While the Listener Functions should look like this

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

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

You cannot pass function calls with parameters as a parameter for another function call. .addEventListener only accepts references to functions. You also shouldn't return Function in your Listener Functions, simply use void and forget about returning anything.

You need to figure out a different way to check your "spawncheck:int" objects. Here's a suggestion: Rewrite your code so that "Notes" are not Movieclips, but Objects from a Class Note. Give the class a public variable spawnCheck and check for it in the EventListener functions.

In general you should relearn the very basics of AS3 coding.

0
votes

As mentioned before your variables scopes are wrong. Declare var note OUTSIDE of the functions and then you will be removing the listeners to the proper variables:

  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
     ....