Skip to main content
Inspiring
February 15, 2017
Answered

Drag and drop matching game... again (sorry)!

  • February 15, 2017
  • 1 reply
  • 492 views

Hi all,

I was hoping not to post about this again, but trouble has hit again.

On the matching game, when the user matches 2 objects and then presses the back button to go to the main menu, the draggable object stays on the screen.

I thought of removing the event listener but this is already done in the code!

Does anyone know how to stop the object from staying on screen?

This shows the object being matched:

This shows the object staying on the main menu:

Thanks,

Jack

    This topic has been closed for replies.
    Correct answer jackh8500410

    Are you sure you shouldn't be using target instead of currentTarget? I'm no AS3 expert, but that event.currentTarget.parent.addChild(event.currentTarget); line may be ripping the drags out of their container, which would explain why they're not going away.


    I've made a quick fix.

    When the frame loads, all of the objects are made visible and when the back button is pressed I've made them invisible.

    Perfecto!

    1 reply

    Legend
    February 15, 2017

    Post your drag start code.

    Also fix that "requiured".

    Inspiring
    February 15, 2017

    Firstly, well noticed on the bad spelling

    So to pickup an object;

    function pickupObject(event: MouseEvent): void {

      event.currentTarget.startDrag();

      event.currentTarget.parent.addChild(event.currentTarget);

      objectoriginalX = event.currentTarget.x;

      objectoriginalY = event.currentTarget.y;

    }

    To drop an object;

    function dropObject(event: MouseEvent): void {

      event.currentTarget.stopDrag();

      var matchingTargetName: String = "target" + event.currentTarget.name;

      var matchingTarget: DisplayObject = getChildByName(matchingTargetName);

      if (event.currentTarget.dropTarget != null && event.currentTarget.dropTarget.parent == matchingTarget) {

      event.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);

      event.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, dropObject);

      event.currentTarget.buttonMode = false;

      event.currentTarget.x = matchingTarget.x;

      event.currentTarget.y = matchingTarget.y;

      } else {

      event.currentTarget.x = objectoriginalX;

      event.currentTarget.y = objectoriginalY;

      }

    }

    Thanks

    Legend
    February 15, 2017

    Are you sure you shouldn't be using target instead of currentTarget? I'm no AS3 expert, but that event.currentTarget.parent.addChild(event.currentTarget); line may be ripping the drags out of their container, which would explain why they're not going away.