Question

When I click in the stage, anywhere, it calls a function that will change one variables value. How do I make it so when I click again it changes back to original value?

public function Example() {
(...)
Modifier = 1;
stage.addEventListener(MouseEvent.CLICK, Happening);
}

public function Happening(event:Event) {
Modifier = 4;
}
Was it helpful?

Solution

How about keeping a seperate boolean variable?

var clicked:Boolean = false;
var Modifier:int = 1;

stage.addEventListener(MouseEvent.CLICK, Happening);

public function Happening(e:MouseEvent):void{
   if(clicked){
     //return to default
     Modifier = 1;
     clicked = false;
   }else{
     Modifier = 4;
     clicked = true;
   }
}

or even simpler

if(Modifier==4){
   Modifier=1;
}else{
   Modifier=4;
}

or in one line

Modifier = (Modifier==4) ? 1 : 4;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top