4 Replies Latest reply on Aug 28, 2006 5:05 PM by CogDev

    Proximity trigger?

    Boghog Level 1
      Hello!

      I want to trigger an event when the cursor gets within a certain distance of an object. Any ideas how to do this? Is there some kind of proximity trigger function, or is it more complicated than that?

      Thanks!
        • 1. Re: Proximity trigger?
          Boghog Level 1
          bump
          • 2. Re: Proximity trigger?
            CogDev Level 1
            if you're looking for a really simple way to do this, you could have an invisible object (the size of which determines your proximity limit) that gets dragged by the mouse (or on enterFrame, its x and y gets set to the mouse x and y). You could then use hitTest() to see if that object has touched something else on your stage.

            You could also use the mouse _x and _y values in an onEnterFrame function to compare them to the coordinates of your target object(s). For this you'd need a distance equation like this:

            (X1 - X2) + (Y1 - Y2) = total distance (my math is horrible, but I think that works).
            • 3. Re: Proximity trigger?
              Boghog Level 1
              quote:

              if you're looking for a really simple way to do this, you could have an invisible object (the size of which determines your proximity limit) that gets dragged by the mouse (or on enterFrame, its x and y gets set to the mouse x and y). You could then use hitTest() to see if that object has touched something else on your stage.


              My ActionScript knowledge isn't that great - do you think you could give me an example of the code I would use for this? Sounds like an excellent idea though. Thanks very much.
              • 4. Re: Proximity trigger?
                CogDev Level 1
                hitTest() is a method of the MovieClip class, and it's pretty well documented in flash's help menu.

                I've included the example that they give.

                To try this, you need to make a new file, and create two new movie clips, one named "circle_mc" and the other one "square_mc". (you select the movie clip and enter the name under "instance name" in the properties)

                For your purposes, all you need to do is make the dragged movie clip invisible (give it the actionscript this.visible = false)

                In this example, the square gets dragged when you click it, so it needs some changes:

                first, you want to swap circle with square, so that you have the same radius all the way around
                second, you want to change the whole onPress function to just square.startDrag(); (remember that in your final movie, you want a circle though)


                I hope that's not too confusing. Let me know if it works for you.

                Here's the example code: