1

I'm just trying to avoid rolling my own dragging functionality. Does anyone know of any libraries out there that have a startDrag() equivalent where you can use, say, a circular radius for the drag bounds, rather than a rectangular box?

Tom Auger
  • 19,421
  • 22
  • 81
  • 104
  • See the following question and answer for circular dragging: [Constrain MovieClip drag to a circle](http://stackoverflow.com/questions/6509716/constrain-movieclip-drag-to-a-circle/6510032#6510032). – Michael Antipin Jul 27 '11 at 08:41

4 Answers4

1

(For circular drag area) - What you need to do is:

a) Mouse_down: Store start position. Start listening to Enter_frame.

b) Enter_Frame: Check distance from mouse position of mouse to start pos (use pythagoras)

c) only move your object if the distance is less than x

d) Mouse_up: Stop listening to enterframe

Tom
  • 7,994
  • 8
  • 45
  • 62
  • Yeah, this is what I meant by "roll my own" and is the approach I would have taken. In the original question I asked whether anyone knew of any libraries that had done this already. I'll leave the question open a bit longer and if no one surfaces with a library that has this built-in I'll accept your answer. – Tom Auger Jul 28 '11 at 12:59
1

You can use a simple circular collision detection routine, it works out the hit area using the radius of the objects and distance between them. Maybe you will have to manually do this calculation in your onDrag method and stop the drag on collision with the circular bounds calculated below.

        var deltax : Number = targetCentreCoord.x - hitTestCentreCoord.x;
        var deltay : Number = targetCentreCoord.y - hitTestCentreCoord.y;

        //works out if our circles are colliding, distance between the circles inc radius               
        if (((deltax * deltax) + (deltay * deltay)) <= ((((targetRadius) + (hitTargetRadius)) * ((targetRadius) + (hitTargetRadius))))) 
        {
            Log.info("collision occured with " + candidate.name + " target coords " + targetCentreCoord + " candidate coords " + hitTestCentreCoord);
            return true;
        } 
        return false;
serenskye
  • 3,467
  • 5
  • 35
  • 51
  • Again, sorry, there are no collisions involved in this question. – Tom Auger Jul 28 '11 at 12:58
  • 1
    You need to do collision detection to detect a hit with the boundaries in your question, then you would stop the drag. Unless I am misunderstanding your question. – serenskye Jul 29 '11 at 11:25
  • Fraid so, I'm not hitting anything. Just need to limit the bounds of the drag. AS3 does this natively but only a rectangular bounding box, not a circular one. Again, the question specifically asks for a library, not a home-grown solution. The home-grown approach is easy, but a library would catch all the edge cases, which is why I was asking. – Tom Auger Jul 29 '11 at 15:18
0

Nope, you need to do pixel-perfect collision (or in this case, mouse clicking) in order to do that. By nature all display objects always have rectangular bounds to them. So basically you'd have to do something like this:

mySprite.addEventListener(MouseEvent.MOUSE_DOWN, mousedDown);

function mousedDown(e:MouseEvent):void
{
    //Draw my sprite to a bitmap, then check the bitmap colour at mouseX/mouseY
    uint colour = myBitmap.getPixel32(mouseX, mouseY);
    if(colour != TRANSPARENT){
        //We've actually clicked on the object, drag it
        Sprite(e.currentTarget).startDrag();
    }
}

Note this is just pseudo code, you'll have to figure out what uint value transparent comes up as, and also you'll have to account for where the origin point of the sprite is when drawing to bitmap. Say you have a sprite and the contents are inside, you're going to need to create a Matrix object that has a X and Y offset that are negative .5 times the width of your sprite in order to draw it properly.

  • Hey thanks for the comment. I think I must have not been very clear in what I was asking. The bounds in question is not meant to determine the hit area of the object (which is done automatically by the way). It's about the boundaries of where I can drag the object to! ie: startDrag(lockCenter:Bool, bounds:Rect). When you specify bounds, you have to specify a Rect beyond which you cannot drag the object (it just stops dragging). This is my desired behaviour BUT I want the bounds to be a circular area not a rectangular one. It doesn't have to do with collisions at all. – Tom Auger Jul 27 '11 at 05:36
  • Ohhhhhhhh lol okay sorry about that. If I can think of or find anything I'll post a new answer. –  Jul 27 '11 at 05:52
-1

This can be done without ENTER_FRAME event. Have a MOUSE_DOWN listener, check boundaries there, if within boundaries, then add a MOUSE_MOVE listener. Also, start out with a MOUSE_UP listener to remove the MOUSE_MOVE listener.