9 Replies Latest reply on Aug 25, 2010 9:45 PM by Rothrock

    Mousedown

    Spaghetee

      Alright, I did some searching around but couldn't find anything really similar to my situation (I was suprised) and with my google search some guy had the same problem, but the solution was a link that is now broken.

       

      How do I make a script on my button that says "If the mouse is down, something happens?"

       

      I don't want it to be on release, but so that it does one thing when the mouse is held down, and another if the mouse is released.

       

      I tried many things, but they all ended up in some error code, so help would be appreciated

       

      Thanks

      Spaghetee

        • 1. Re: Mousedown
          Rothrock Level 5

          Really depends upon what you mean by this. People often confuse onMouseDown/onMouseUp with onPress/onRelease. The mouse versions just listen for the mouse going down and up, whereas onPress/onRelease check to see if the mouse has been clicked up/down ON a specific movieclip or button.

           

          So, make two movieclips called myClip0 and myClip1

           

          myClip0.onPress=tracePress;

          myClip1.onPress=tracePress;

          myClip0.onMouseDown=traceDown;

          myClip1.onMouseDown=traceDown;

           

          function tracePress(){

          trace(this._name+" was pressed.");

          }

           

          function traceDown(){

          trace("The mouse goes down and "+this._name+" knows it.");

          }

           

          And you will see how the traces are different depending upon if you click down on one of the clips or not. So hopefully that may answer any first bit of your question. I'm going to assume that you want the onMouseDown. So just do what you want to happen:

           

           

          var home:MovieClip=this;

           

          home.onMouseDown=startDown;

          home.onMouseUp=stopDown;

           

           

          function startDown(){

          //do what you want;

          }

           

          function stopDown(){

          //stop what you want

          }

           

          Now you might want something to happen continuously while the mouse is down. Not sure what that might be, but you could use an onEnterFrame, then your functions might look like this:

           

          function startDown(){

          home.onEnterFrame=doAbunch;

          }

           

          function stopDown(){

          delete home.onEnterFrame;

          }

           

          function doAbunch(){

          trace("I'm doing something over and over until you let go!");

          }

           

          So try that out and come back if you have questions.

          • 2. Re: Mousedown
            Spaghetee Level 1

            I didn't know about onPress, but I suppose it could work. I just want something to appear when the mouse button is pressed, and when it is released for a series of things to happen, but I think I have the release part down

             

            When I tried the code, I got many errors saying how it needed to be with an "on handler"

             

            But if onPress would work, I'd like to try that

             

            -Spaghetee

            • 3. Re: Mousedown
              Rothrock Level 5

              You are trying to put code directly onto the items you are selecting. Don't do that. That is an old style of coding that went out of favor in 2001 with the introduction of Flash 6. Here is a really good blog article about the difference and why the old way isn't so great:

               

              http://www.quip.net/blog/2006/flash/museum-pieces-on-and-onclipevent

               

               

               

              The code I'm showing you should be placed on the main timeline.

               

              Do you care if the mouse button is pressed when the pointer is on top of a specific movieclip or when the mouse button is pressed regardless of where the pointer is? The first one you want onMouseDown and the second one you want onPress.

              1 person found this helpful
              • 4. Re: Mousedown
                Spaghetee Level 1

                The main timeline = the layer that the button is on?

                 

                EDIT: It has to be pressed at that specific movieclip, not just anywhere

                • 5. Re: Mousedown
                  Rothrock Level 5

                  Yes the same timeline that the "button" is on. So in that case then you would want to use onPress not onMouseDown.

                   

                  When you say "button" do you mean an actual button instance? Very few people I know actually use buttons. There are some funky strange things about buttons and how they work and what scope they are in. And if you need some movieclip inside a button you won't be able to reference it. So there are so many things that are just odd/inconvenient with them. Most folks I know stopped using them with Flash 6 when you could start adding mouse events to movieclips.

                  • 6. Re: Mousedown
                    Spaghetee Level 1

                    Yes, I am using an actual button for the sole reason that it made sense in my head that if I was making a button, I should make it a button, and not a movieclip

                     

                    Anyways, if you say that I should use a movieclip I will. This button is essentially a start button, but something is displayed onPress, and the game does not start until onRelease...

                     

                    How would I go about using onPress? The same as onRelease?

                     

                    -Spaghetee

                    • 7. Re: Mousedown
                      Rothrock Level 5

                      Yeah just like in the examples I showed above.

                       

                      BTW, you might also want to do something with onReleaseOutside as well...

                       

                      If you haven't figured out how all this works, I recommend to put aside your current project and make a new file with just a couple simple parts so you can see how it all works without the added complications. Then once you get a handle on it, move it into your project.

                      • 8. Re: Mousedown
                        Spaghetee Level 1

                        Instead of fussing around with layers, buttons, etc. I put the code in my main character's class. I figured it would be easier because I can understand and prevent errors more there (I'm more comfortable putting stuff there-)

                         

                        I did something like this:

                         

                        _root.startButton.onPress = function()
                        {
                            _root.supriseMessage._visible = true;
                        }

                         

                        _root.startButton.onRelease = function()
                        {

                        _root.supriseMessage._visible = false;

                        // I also made everything else false or true in correspondence of how the game needed things to be to run properly   
                        }

                         

                        Thanks though, I would have spent much time researching mouseDown only to figure out it doesn't work for my case

                        -Spaghetee

                        • 9. Re: Mousedown
                          Rothrock Level 5

                          If it works for you then great. There are a lot of things about this that aren't best practices and will make things more difficult for you down the line.

                           

                          Generally you shouldn't use absolute paths from _root. It will make problems if you later load swfs into other swfs.

                           

                          Also it isn't good practice to use anonymous functions. Notice how your functions in your event handler assignments don't have names. That makes it impossible to reuse them. So:

                           

                          myButton.onPress=handlePress

                           

                          function handlePress(){

                          // do something

                          }

                           

                          will take you down the path toward better coding.

                           

                          Also you might want to look into onReleaseOutside. If you press the mouse down on startButton, but then move the pointer outside of the button and release you probably won't like what you see.

                           

                          These are all just suggestions from a lot of years and a lot of mistakes....