开发者

AS3 How to make a button toggle a function?

开发者 https://www.devze.com 2023-03-31 16:19 出处:网络
When button1 is clicke开发者_开发百科d, the cursor changes into a \'movieclip\' I want this movieclip cursor to switch back into a regular cursor

When button1 is clicke开发者_开发百科d, the cursor changes into a 'movieclip' I want this movieclip cursor to switch back into a regular cursor when button1 is clicked again, so toggle the function on and off.

My question to you is, is it likely to use some kind of boolean here to toggle the function on and off or am I in the wrong direction? Thanks in advance!

button1.addEventListener(MouseEvent.CLICK,wipe);

function wipe(e:Event):void 
{

        Mouse.hide();
        stage.addEventListener(MouseEvent.MOUSE_MOVE,follow);
        function follow(evt:MouseEvent)
              {
              cursor.x = mouseX;
              cursor.y = mouseY;
              }
}


Try

protected function wipe(e:Event):void {
    if (stage.hasEventListener(MouseEvent.MOUSE_MOVE, follow) {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, follow);
    } else {
        stage.addEventListener(MouseEvent.MOUSE_MOVE, follow);
    }
}

I'd move the definition of follow out of wipe. Does that even work?

If you need to have other listeners for MOUSE_MOVE directly on the stage then you might want to go with something more like:

protected var isFollowing:Boolean;
protected function wipe(e:Event):void {
    if (isFollowing) {
       stage.removeEventListener(MouseEvent.MOUSE_MOVE, follow);
   } else {
        stage.addEventListener(MouseEvent.MOUSE_MOVE, follow);
   }
   isFollowing = !isFollowing;
}

Note: I've assumed you're going to put your code into a document Class, since I always assume that if you care enough to ask on a place like Stack Overflow, you'd like to use good practice.


var isMC:Boolean=false;


button1.addEventListener(MouseEvent.CLICK,wipe);
cursor.visible=false;
function wipe(e:Event):void 
{
if(isMC==false){
    cursor.visible=true;
    Mouse.hide();
    stage.addEventListener(MouseEvent.MOUSE_MOVE,follow);
    function follow(evt:MouseEvent)
          {
          cursor.x = mouseX;
          cursor.y = mouseY;

          }
          isMC=true;}else{Mouse.show();
          cursor.visible=false;
          isMC=false

}}   

Hope this helps.

0

精彩评论

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