简体   繁体   中英

How to make an On/Off Function

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM