开发者

Flash AS3 toggle visible

i have a script that once clicked ( a button ) some other thing hides then once clicked again it re-shows. the problem is once hidden it never shows again here is the script:

menu_start.addEventListener(MouseEvent.CLICK, myClickFunction);


function myClickFunction(event:MouseEvent) {

      // Hide the first and show the next here
      if (menu_menu.visible == true){
      menu_menu.visible = false;

      } 
      if (menu_menu.visib开发者_JAVA技巧le == false) {
          menu_menu.visible == true;
      }

}

Thanks so much.


I prefer to write such logic in short form:

menu_menu.visible = !menu_menu.visible;


The reason is when you click on the button, it does hide but again when you click on the same button it does not show back

Correct me if i am wrong in the above statement.

Now try something on what i say, have two buttons Hide and Show. Create two new function and try it out, if this works then there is something you are missing in your logic, if this does not work then let us know.

Also try this.

function myClickFunction(event:MouseEvent) {

      // Hide the first and show the next here
      if (menu_menu.visible){
      menu_menu.visible = false;

      } else {
         menu_menu.visible = true;
      }

}

The other issue could be, when you click on the button may be its not getting the menu_menu property again as its hidden or destroyed. Is it inside the same component or called from somewhere else?


In your second "if" statement you are not assigning .visible to true but instead checking to see if it's equal to true because of the two equal signs.

function myClickFunction(event:MouseEvent) {

  // Hide the first and show the next here
  if (menu_menu.visible == true){
  menu_menu.visible = false;

  } 
  if (menu_menu.visible == false) {
      menu_menu.visible = true;
  }

}


Heres a better version based on Glens suggestion that I made, feel free to use.

Buttonname.addEventListener (MouseEvent.CLICK, FunctionName);
function FunctionName(event:MouseEvent) {

  if (name1.alpha == 1){
  name1.alpha = 0;} else {name1.alpha = 1}
}

What this script is saying is if name1 (a Movie Clip symbol) has an Alpha value equal to 1 when clicked then it will then change the Alpha value to 0, otherwise it will change the Alpha value to 1.

This will also work with the "visible" functions:

Buttonname.addEventListener (MouseEvent.CLICK, FunctionName);
function FunctionName(event:MouseEvent) {

  if (name1.visible == true){
  name1.visible = false;} else {name1.visible = true}
}


Try using alpha = 0.1 instead of visible = false and alpha = 1 instead of visible = true.

The problem is that when you use visible = false it also disables mouse interaction so your second click never fires.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜