12 Replies Latest reply on Mar 13, 2011 7:43 PM by Ned Murphy

    IF mouseOver  statement

    mdermez Level 1

       

       

      I have this:

       

      function ballspeed(event:MouseEvent):void{
      thatball.nextFrame();
      }

       

      thatball.addEventListener(MouseEvent.MOUSE_OVER, ballspeed);

       

      but I want to say the same thing with an IF condition.

       

      "if the mouse its over that movieclip (thatball) then go to next frame ( thatball.nextFrame(); )

      (only IF)

      Whats the code for that?
        • 1. Re: IF mouseOver  statement
          Ned Murphy Adobe Community Professional & MVP

          Here's one way, though it will not be vigilante without other code to keep it constantly checking.  You'll need to have additional code to have it constantly checking.  The mouseover code you showed is a more efficient usage.

           

          if(thatball.hitTestPoint(mouseX,mouseY,true))  thatball.nextFrame();

          1 person found this helpful
          • 2. Re: IF mouseOver  statement
            mdermez Level 1
            ////Thanks for your code but it doesnt work. I found this one and it works. What do you think?
            var mouseOver:Boolean = false;
            thatball.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
            thatball.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);

             

            function mouseOverHandler(e:MouseEvent):void{
                mouseOver = true;
            }

             

            function mouseOutHandler(e:MouseEvent):void{
                mouseOver = false;
            }

            addEventListener(Event.ENTER_FRAME, update);

             

            function update(e:Event):void
            {
            if(mouseOver){
            thatball.stop();
            } else{
            thatball.play();
            }

             

            }

            • 3. Re: IF mouseOver  statement
              Ned Murphy Adobe Community Professional & MVP

              What I provided will work, but you have to know what you're doing to use it--and it does all that you asked for.  An if statement will only execute once as soon as it is encountered, so if the conditions are not what they should be when it executes, then it will not do anything.

               

              I don't want to come off as insulting but the solution you have is a waste of effort, both in coding it and for the Flash player to have to process it.  That enterframe functionality will be processing constantly (and is what you would have needed to make what I provided work as you seem to want it to).  The code you showed can be simplified to...

               

              thatball.play();

              function mouseOverHandler(e:MouseEvent):void{
                  thatball.stop();
              }

               

              function mouseOutHandler(e:MouseEvent):void{
                  thatball.play();
              }
              thatball.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
              thatball.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);

               

               

              1 person found this helpful
              • 4. Re: IF mouseOver  statement
                mdermez Level 1

                But I tested it and it didnt worked.

                 

                I tried this:

                 

                if(thatball.hitTestPoint(mouseX,mouseY,true)){
                thatball.stop(); ////I wanted it to stop just to check if the code is working
                }else{
                thatball.play();
                }

                 

                /////But it doesnt!!!!! While the mouse is over the clip it keeps playing. It doesnt stop.

                • 5. Re: IF mouseOver  statement
                  mdermez Level 1

                  By the way the simplified code that you posted its also working and I liked it that way (thanks)

                   

                  By the way do you know any way that the mouseOver condition could be a mouseHover condition? I would like the function to run not only when the mouse is just once over the clip - because in order to re-execute the function I have to place the mouse over the clip again. I want the function to run while !!!!! the mouse is on the clip.

                   

                  Thank you in advance again.

                  • 6. Re: IF mouseOver  statement
                    Ned Murphy Adobe Community Professional & MVP

                    Yep, read what I already said a couple times.  An if statement only executes ONCE when it is encountered.  If you had that code in your frame, it executed as soon as you entered that frame and was done working... it does not sit there waiting for the mouseover condition to happen... that's what event listeners do.

                     

                    You asked for a solution to a few lines of code using an if() statement, and that is what I provided.  The code you showed later provided a more complete picture of what you were trying to do. Your decision for needing an "if" version of those few lines was wrong.

                    • 7. Re: IF mouseOver  statement
                      mdermez Level 1

                      Yeap I was wrong on that. Do you have any idea about how can I make it from mouseOver to MouseHover? Thanks

                      • 8. Re: IF mouseOver  statement
                        Ned Murphy Adobe Community Professional & MVP

                        How is MouseHover different from MouseOver (there s no such thing as MouseHover, so I have to ask)?

                        • 9. Re: IF mouseOver  statement
                          mdermez Level 1

                          I know that there is no Hover condition in flash. The difference is that in hover condintion a function runs as long as the cursor is over a clip. In over condition the function runs when the cursor is on the clip. It runs just once, In order to run again the cursor must be placed again over the clip.

                          • 10. Re: IF mouseOver  statement
                            Ned Murphy Adobe Community Professional & MVP

                            You would use the mouseover handler to trigger an enterframe listener.  The enterframe event handler would take care of continually processing whatever it is you want to do.   You would then use the mouseout handler to remove the enterframe, assuming you want it to stop whatever it is doing.  You have already attempted to implement an enterframe event, so you should be able to experiment and see what you can get working.

                            • 11. Re: IF mouseOver  statement
                              mdermez Level 1

                              Something like this? Is it right?

                               

                              thatball.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);

                              thatball.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);

                               

                              function mouseOverHandler(e:Event):void{
                                  addEventListener(Event.ENTER_FRAME, ballspeed);
                              }

                               

                              function mouseOutHandler(e:Event):void{
                                  removeEventListener(Event.ENTER_FRAME, ballspeed);
                              }

                               

                              function ballspeed(event:Event):void{
                              ///do something
                              }

                              • 12. Re: IF mouseOver  statement
                                Ned Murphy Adobe Community Professional & MVP

                                Yep, you should have tried it to see... though I hesitate to ask what you plan for "do something"