# Dragging an item over another

**URL:** <https://forum.kirupa.com/t/dragging-an-item-over-another/261188>\
**Category:** programming\
**Created:** [May 23, 2008, 11:58pm UTC](https://forum.kirupa.com/t/dragging-an-item-over-another/261188 "2008-05-23T23:58:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![onion](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/onion/32/3349_2.png) [@onion](https://forum.kirupa.com/u/onion)\
**Post date:** [May 23, 2008, 11:58pm UTC](https://forum.kirupa.com/t/dragging-an-item-over-another/261188/1 "2008-05-23T23:58:12Z")

</div>

I want to drag a picture over a grey box, and when I release the mouse button the picture to appear in the gray box. Easy peasy so far, right?

Well I also want the grey box to have an outline appear when you are dragging the item over it, but the outline to disappear when you move it away again.

rollOver events aren’t triggered when you are dragging an object, so I was wondering what the best and most efficient way of doing this is. I was thinking of running a hitTest every frame to see if our dragging box is touching a grey box, but that seems inefficient. Also there will be up to 10 grey boxes - and I don’t want to run a hitTest to check for 10 boxes every frame!

Can anyone suggest the best method in AS2 please?

---

<div class="post-metadata">

**Author:** ![bluemagica](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bluemagica](https://forum.kirupa.com/u/bluemagica)\
**Post date:** [May 24, 2008, 5:39am UTC](https://forum.kirupa.com/t/dragging-an-item-over-another/261188/2 "2008-05-24T05:39:06Z")

</div>

check distance from mouse.

---

<div class="post-metadata">

**Author:** ![onion](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/onion/32/3349_2.png) [@onion](https://forum.kirupa.com/u/onion)\
**Post date:** [May 26, 2008, 9:41pm UTC](https://forum.kirupa.com/t/dragging-an-item-over-another/261188/3 "2008-05-26T21:41:24Z")

</div>

not a bad idea, but I still have to run a check for that on every box on every frame

---

<div class="post-metadata">

**Author:** ![tapioca](https://avatars.discourse-cdn.com/v4/letter/t/a4c791/32.png) [@tapioca](https://forum.kirupa.com/u/tapioca)\
**Post date:** [May 30, 2008, 5:43am UTC](https://forum.kirupa.com/t/dragging-an-item-over-another/261188/4 "2008-05-30T05:43:09Z")

</div>

does 10 hittests a frame really lag that much? i think most games do a lot more than that.

---

<div class="post-metadata">

**Author:** ![onion](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/onion/32/3349_2.png) [@onion](https://forum.kirupa.com/u/onion)\
**Post date:** [May 30, 2008, 10:04am UTC](https://forum.kirupa.com/t/dragging-an-item-over-another/261188/5 "2008-05-30T10:04:38Z")

</div>

> [@tapioca;2333453](#):
>
> does 10 hittests a frame really lag that much? i think most games do a lot more than that.

The fewer the better, you haven’t seen how much else I have going on per frame!  
Also imagine this is going in a browser, with other flash things all around it like adverts etc, thats a lot for the browser to do. So i’m trying to cut down everything I can so that it will run smoothly.

---

<div class="post-metadata">

**Author:** ![tapioca](https://avatars.discourse-cdn.com/v4/letter/t/a4c791/32.png) [@tapioca](https://forum.kirupa.com/u/tapioca)\
**Post date:** [May 30, 2008, 7:34pm UTC](https://forum.kirupa.com/t/dragging-an-item-over-another/261188/6 "2008-05-30T19:34:52Z")

</div>

well, the two optimizy type things that i would probably do is put all of the grey boxes into their own mc, then hittest that to see if any of them were hit and if one is, hittest all the little boxes individually to see which one specifically was hit. and put the hittest on onmousemove instead of enterframe. i dun rmmbr how to do mousey type stuff so this code probably doesn’t work but you can fix it.

```auto

// put all boxes in an mc called boxContainer
// box instances are in an array called boxArray

onMouseMove() {
  if (boxContainer.hitTest(mouse_x, mouse_y, true) {
    for (i = 0; i < 10; i++) {
      if (boxArray*.hitTest(mouse_x, mouse_y, false) {
        // do your hit thingy here
        break;
      }
    }
  }
}

```

i heard putting all your hittests in one mc is faster but i’ve never actually tested it, so idk, try it for me maybe. 😛

---

<div class="post-metadata">

**Author:** ![bpalermo](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bpalermo](https://forum.kirupa.com/u/bpalermo)\
**Post date:** [May 31, 2008, 3:12pm UTC](https://forum.kirupa.com/t/dragging-an-item-over-another/261188/7 "2008-05-31T15:12:33Z")

</div>

Doesn’t seem to make much sense for me. Obviously, I may be blatantly wrong. It’s just a thought…

---

<div class="post-metadata">

**Author:** ![bpalermo](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bpalermo](https://forum.kirupa.com/u/bpalermo)\
**Post date:** [May 31, 2008, 3:13pm UTC](https://forum.kirupa.com/t/dragging-an-item-over-another/261188/8 "2008-05-31T15:13:40Z")

</div>

Doesn’t seem to make much sense for me. Obviously, I may be blatantly wrong. It’s just a thought…

---

<div class="post-metadata">

**Author:** ![cooldude88](https://avatars.discourse-cdn.com/v4/letter/c/e0b2c6/32.png) [@cooldude88](https://forum.kirupa.com/u/cooldude88)\
**Post date:** [May 31, 2008, 8:10pm UTC](https://forum.kirupa.com/t/dragging-an-item-over-another/261188/9 "2008-05-31T20:10:35Z")

</div>

i dont think that one mc is the way to go because ive found that unless you call the items individuakllyt anyway it just hit tests everything from the most extreme boundy to the other of those items. one idea would be to write a class i personally am not sure how to do that but i heard its faster and more effiecient way of going at thnigs.

---

<div class="post-metadata">

**Author:** ![tapioca](https://avatars.discourse-cdn.com/v4/letter/t/a4c791/32.png) [@tapioca](https://forum.kirupa.com/u/tapioca)\
**Post date:** [June 1, 2008, 5:58am UTC](https://forum.kirupa.com/t/dragging-an-item-over-another/261188/10 "2008-06-01T05:58:26Z")

</div>

cooldude: the last argument in hitTest is a shape flag that tells it to test on outside bounds if it’s false or actual shape if it’s true.
