Iura Rusu
Iura Rusu

Reputation: 95

Removing EventListeners in Actionscript 3

I have a function in which I want to remove an EventListener, but it gives me the following error:

Access of undefined property event

Here is the code in question:

dr_line.addEventListener(MouseEvent.CLICK,drawln);
var test:Boolean;

function drawln(e:MouseEvent):void{
    event.currentTarget.removeEventListener(MouseEvent.CLICK, drawln);
    stage.addEventListener(MouseEvent.CLICK,click1);    
}

var sx,sy,fx,fy,j:int;

function click1(e:MouseEvent):void{
    sx=mouseX;
    sy=mouseY;
    stage.addEventListener(MouseEvent.CLICK,click2);
}

function click2(e:MouseEvent):void{
    var i:int;
    i=1;
    trace(i);
    fx=mouseX;
    fy=mouseY;
    var  line:Shape = new Shape();
    line.graphics.beginFill(0x00FF00);
    line.graphics.moveTo(sx,sy);
    line.graphics.lineTo(fx,fy);
    this.addChild(line);
}

I tried doing the same removal of the event listener in click1 and click2, but it still doesn`t work.

What am I doing wrong?

Upvotes: 0

Views: 45

Answers (1)

Panzercrisis
Panzercrisis

Reputation: 4750

event is not declared; e is. Try changing this:

function drawln(e:MouseEvent):void{
    event.currentTarget.removeEventListener(MouseEvent.CLICK, drawln);
    stage.addEventListener(MouseEvent.CLICK,click1);    
}

to this:

function drawln(e:MouseEvent):void{
    e.currentTarget.removeEventListener(MouseEvent.CLICK, drawln);
    stage.addEventListener(MouseEvent.CLICK,click1);    
}

or possibly even this:

function drawln(e:MouseEvent):void{
    dr_line.removeEventListener(MouseEvent.CLICK, drawln);
    stage.addEventListener(MouseEvent.CLICK,click1);    
}

Upvotes: 1

Related Questions