Dragging an item over another

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?

check distance from mouse.

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

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.

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.


// 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. :stuck_out_tongue:

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

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

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.

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.