0

I've created a drag and drop "puzzle" that has 16 unique pieces, each with their own instance name.

The problem I have is that there are 4 target zones made up of arrays (4 pieces per zone, but the order of the pieces in the zone does not matter). When a piece gets dropped into its correct zone, I would like to "switch" it with another movieclip and have that new movieclip be in the drop target area.

So, for each of the 16 puzzle pieces, I also have 16 unique companion pieces that need to somehow be paired up so that when the visible puzzle piece gets dropped, it is both removed from view, but also replaced with its companion piece.

Any ideas on how to do this?

  • 1
    Can you explain where, specifically, you're getting stuck? This is a fairly simple task, but it would help to know how to phrase an answer. – Marcela Mar 14 '13 at 19:26
  • Yes, I'm not exactly sure how to "pair" the two different movieclips together, since the pairs will have one unique instance name for the puzzle piece, and another different unique instance name of the piece that's supposed to replace it. Would I be dragging both movieclips at once and then toggling visibility of one over the other, or would I somehow keep the "paired" replacement in the library and have it replace the x/y coordinates once the puzzle is dropped? – user2171261 Mar 14 '13 at 20:55

1 Answers1

0

MovieClip is a dynamic class, meaning that you can add properties to it at runtime. Leveraging this, you can assign a property to your original pieces and call it something like pairedPiece. In this property, you'll store the appropriate value (the name of its pair in the library).

var firstMovieClip:MovieClip;
// do whatever you need to set up your firstMovieClip, attach listeners, etc
firstMovieClip.pairedPiece = "SecondMovieClip";

// the following will occur when the piece is dropped and you need to swap it
var secondMovieClip:MovieClip = new (getDefinitionByName(firstMovieClip.pairedPiece) as Class)() as MovieClip;
secondMovieClip.x = firstMovieClip.x;
secondMovieClip.y = firstMovieClip.y;
firtMovieClip.parent.addChildAt(secondMovieClip, firstMovieClip.parent.getChildIndex(firstMovieClip));
firstMovieClip.parent.removeChild(firstMovieClip);
Marcela
  • 3,728
  • 1
  • 15
  • 21