Skip to main content
Participating Frequently
July 13, 2016
Question

Drag and Drop, unable one object to be droped on target if other is already in there.

Hi, i have 2 objects (named pieces) and 2 targets. I want them to be able to be droped on both targets, but unable the other to be droped if one target has already an object in it.

Further, i want to aknowledge that the targets are with objects, so one button can appear.

Excuse my poor english and thanks in advance,

heres the code:

import flash.events.Event;

import flash.events.MouseEvent;

stage.addEventListener(Event.ENTER_FRAME,EntFrame2);

function EntFrame2(e:Event):void

{

piece001_mc.buttonMode = true;

piece001_mc.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_001);

function fl_ClickToDrag_001(event:MouseEvent):void

{

  piece001_mc.startDrag();

  stage.addEventListener(MouseEvent.MOUSE_UP,fl_ReleaseToDrop_001);

}

function fl_ReleaseToDrop_001(event:MouseEvent):void

{

  piece001_mc.stopDrag();

  stage.removeEventListener(MouseEvent.MOUSE_UP,fl_ReleaseToDrop_001);

if (targetA_mc.hitTestObject(piece001_mc.tar001_mc))

{

piece001_mc.x = target001_mc.x;

piece001_mc.y = target001_mc.y;

//play sound correct answer

channel1 = mySound.play();

channel1.soundTransform = volumeAdjust;

}

else

{

piece001_mc.x = else001.x;

piece001_mc.y = else001.y;

//play sound wrong answer

channel2 = mySound2.play();

channel2.soundTransform = volumeAdjust2;

}

}

piece002_mc.buttonMode = true;

piece002_mc.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_002);

function fl_ClickToDrag_002(event:MouseEvent):void

{

  piece002_mc.startDrag();

  stage.addEventListener(MouseEvent.MOUSE_UP,fl_ReleaseToDrop_002);

}

function fl_ReleaseToDrop_002(event:MouseEvent):void

{

  piece002_mc.stopDrag();

  stage.removeEventListener(MouseEvent.MOUSE_UP,fl_ReleaseToDrop_002);

if (targetA_mc.hitTestObject(piece002_mc.tar002_mc))

{

piece002_mc.x = target002_mc.x;

piece002_mc.y = target002_mc.y;

//play sound correct answer

channel1 = mySound.play();

channel1.soundTransform = volumeAdjust;

}

else

{

piece002_mc.x = else002.x;

piece002_mc.y = else002.y;

//play sound wrong answer

channel2 = mySound2.play();

channel2.soundTransform = volumeAdjust2;

}

}

if (piece001_mc.x == target001_mc.x&&

piece002_mc.x == target002_mc.x)

{

p6.visible = true;

sig7.visible = true;

stage.removeEventListener(Event.ENTER_FRAME,EntFrame1);

}

else

{

p6.visible = false;

sig7.visible = false;

}

}

This topic is closed to new replies. Start a new post to keep the conversation going.

5 replies

kglad
Community Expert
Community Expert
July 13, 2016

the first problems i see are you've nested named functions and you're using an enterframe loop incorrectly.

unnest all named functions.

remove your enterframe listener unless it's needed for something and, in that case, only include what's needed in the listener function.  that would look more like:

piece001_mc.buttonMode = true;

piece001_mc.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_001);

piece002_mc.buttonMode = true;

piece002_mc.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_002);

function fl_ClickToDrag_001(event: MouseEvent): void {

    piece001_mc.startDrag();

    stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_001);

}

function fl_ReleaseToDrop_001(event: MouseEvent): void {

    piece001_mc.stopDrag();

    stage.removeEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_001);

    if (targetA_mc.hitTestObject(piece001_mc.tar001_mc)) {

        piece001_mc.x = target001_mc.x;

        piece001_mc.y = target001_mc.y;

        //play sound correct answer

        channel1 = mySound.play();

        channel1.soundTransform = volumeAdjust;

    } else {

        piece001_mc.x = else001.x;

        piece001_mc.y = else001.y;

        //play sound wrong answer

        channel2 = mySound2.play();

        channel2.soundTransform = volumeAdjust2;

    }

    checkForEndF();

}

function fl_ClickToDrag_002(event: MouseEvent): void {

    piece002_mc.startDrag();

    stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_002);

}

function fl_ReleaseToDrop_002(event: MouseEvent): void {

    piece002_mc.stopDrag();

    stage.removeEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_002)

    if (targetA_mc.hitTestObject(piece002_mc.tar002_mc)) {

        piece002_mc.x = target002_mc.x;

        piece002_mc.y = target002_mc.y;

        //play sound correct answe

        channel1 = mySound.play();

        channel1.soundTransform = volumeAdjust;

    } else {

        piece002_mc.x = else002.x;

        piece002_mc.y = else002.y;

        //play sound wrong answer

        channel2 = mySound2.play();

        channel2.soundTransform = volumeAdjust2;

    }

    checkForEndF();

}

function checkForEndF(): void {

    if (piece001_mc.x == target001_mc.x && piece002_mc.x == target002_mc.x) {

        p6.visible = true;

        sig7.visible = true;

    } else {

        p6.visible = false;

        sig7.visible = false;

    }

}

Translate
krakk356Author
Participating Frequently
July 14, 2016

Thanks for organize the code, i've been following tutorials and done stuff by myself but i don't understand much of it.

Still, the problem perssists, i want the piece 1 and piece 2 be able to be droped in both targets 1 or 2, but if one of them is already there, the other must go for the target that is free.

PS: i just write part of the code, actualy it goes further of 3 objects that can be droped in 3 targets, but i think if i undertand how 2 works i can make 3 work.

Translate
kglad
Community Expert
Community Expert
July 14, 2016

use the trace function to debug your code.

Debugging ActionScript That Triggers No Errors |Adobe Community

Translate