1

Hi so recently I have been attempting to Drag a movie clip in AS3 but I'm having some trouble picking up with the hit tests anyone got any ideas? Just to clarify, the issue is that when the movieclips hit the drag test object, they're not executing the gotoframe() function. initDrag() adds action listeners: MOUSE_DOWN on the object MOUSE_UP on the stage so it doesn’t matter if you are off the object

endDrag() removes the action listeners; call this (for each object) before you go to another frame

startADrag()create a rectangle within which the object can be dragged (in this case the stage) call startDrag() on the object

stopADrag() call stopDrag() on the object from currentObject (but only if currentObject is not null).

var currentObject:MovieClip = null;
initDrag(block1);
initDrag(block2);
initDrag(block3);
initDrag(block4);

function initDrag(obj:MovieClip )
{
    obj.addEventListener(MouseEvent.MOUSE_DOWN,startADrag);
    stage.addEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function endDrag(obj:MovieClip )
{
    obj.removeEventListener(MouseEvent.MOUSE_DOWN,startADrag);
    stage.removeEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function startADrag(e:MouseEvent):void
{
    currentObject = (MovieClip)(e.target);
    var rect:Rectangle = new Rectangle(0,0,stage.stageWidth - currentObject.width,stage.stageHeight - currentObject.height + 100);
    currentObject.startDrag(false,rect);
}
function stopADrag(e:MouseEvent):void
{
    if (currentObject != null)
    {
        currentObject.stopDrag();
    }
}
if(block1.hitTestObject(dragtest)){
gotoAndStop("lose");
}
if(block2.hitTestObject(dragtest)){
    gotoAndStop(27);
}
if(block3.hitTestObject( dragtest)){
    gotoAndStop("lose");

}
if(block4.hitTestObject( dragtest)){
gotoAndStop("lose");
}

thanks for any advice or answers.

Trows
  • 391
  • 2
  • 12
  • Your dragging code looks correct, could you elaborate on the problem with the hit tests? – Iggy Oct 24 '14 at 01:30
  • @Iggy hitTestObject is not detecting and changing the frame. So, when I drag the movieclip onto object dragtest, it should change the frame to "lose" or 27 – Trows Oct 24 '14 at 02:11
  • Well for one you should place all your if statements inside an event loop. – Iggy Oct 24 '14 at 02:20
  • Will that do anything? – Trows Oct 24 '14 at 03:00
  • Yes. Because those if statements get executed once and that's it. In fact, they get executed before you even start dragging. implement an event listener for `Event.ENTER_FRAME` or `MouseEvent.MOUSE_MOVE`. put the if statements in there, so that the loop checks for every frame if one of the blocks is hitting your `dragtest` object. –  Oct 24 '14 at 05:54

1 Answers1

2

The following code should work as expected. The problem is, as i already stated in my comment, that your calls to hitTestObject(obj) only get executed once, at the very beginning of your application. What you need to do though is to check it constantly.

Think about it, if your calls to hitTestObject-calls only get executed once at the beginning, when you didn't even have a chance to drag one of your objects, it will always return false, right? Because your objects are still in their initial position (outside of the dragtest objecti must assume).

With an event listener for Event.ENTER_FRAME you check it once per frame instead. So even if all the results for hitTestObject are false, it will check them all again on the next frame (if you are currently dragging, controlled through a simple boolean called dragging).

var currentObject:MovieClip = null;
var dragging:Boolean = false;
initDrag(block1);
initDrag(block2);
initDrag(block3);
initDrag(block4);

addEventListener(Event.ENTER_FRAME, checkForHit);

function checkForHit(e:Event):void{
    if(dragging){
        if(block1.hitTestObject(dragtest)){
             gotoAndStop("lose");
        }
        if(block2.hitTestObject(dragtest)){
             gotoAndStop(27);
        }
        if(block3.hitTestObject( dragtest)){
            gotoAndStop("lose");
        }
        if(block4.hitTestObject( dragtest)){
            gotoAndStop("lose");
        }
    }
}

function initDrag(obj:MovieClip )
{
    obj.addEventListener(MouseEvent.MOUSE_DOWN,startADrag);
    stage.addEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function endDrag(obj:MovieClip )
{
    obj.removeEventListener(MouseEvent.MOUSE_DOWN,startADrag);
    stage.removeEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function startADrag(e:MouseEvent):void
{
    currentObject = (MovieClip)(e.target);
    var rect:Rectangle = new Rectangle(0,0,stage.stageWidth - currentObject.width,stage.stageHeight - currentObject.height + 100);
    currentObject.startDrag(false,rect);
    dragging = true;
}
function stopADrag(e:MouseEvent):void
{
    if (currentObject != null)
    {
        currentObject.stopDrag();
        dragging = false;
    }
}