如何制作开/关功能

时间:2014-04-06 20:31:47

标签: actionscript-3 events click mouseevent flashdevelop

当我在舞台上点击任何地方时,它会调用一个会改变一个变量值的函数。当我再次点击它时,如何改变原始值?

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

public function Happening(event:Event) {
Modifier = 4;
}

1 个答案:

答案 0 :(得分:2)

如何保留一个单独的布尔变量?

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

甚至更简单

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

或一行

Modifier = (Modifier==4) ? 1 : 4;