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