Hi.
I think some encapsulation and other object oriented programming approaches may help a little bit. For example, using containers in different frames and also custom properties on the items instead of variables.
Here is a suggestion in which I tried to make your code more dynamic and less DRY but not too much complex.
AS3 code: [Frame 1]
import flash.display.MovieClip;
import flash.events.Event;
var matches:uint;
var totalMatches:uint;
function game1():void
{
matches = 0;
totalMatches = 0;
setPiece(board1.item1_mc, board1.dropZone1_mc);
setPiece(board1.item2_mc, board1.dropZone2_mc);
setPiece(board1.item3_mc, board1.dropZone3_mc);
setPiece(board1.item4_mc, board1.dropZone4_mc);
setPiece(board1.item5_mc, board1.dropZone5_mc);
setPiece(board1.item6_mc, board1.dropZone6_mc);
stop();
}
function setPiece(target:MovieClip, dropZone:MovieClip):void
{
target.addEventListener(MouseEvent.MOUSE_DOWN, dragHandler);
target.addEventListener(MouseEvent.MOUSE_UP, releaseHandler);
target.buttonMode = true;
target.dropZone = dropZone;
target.originX = target.x;
target.originY = target.y;
totalMatches++;
}
function dragHandler(event:MouseEvent):void
{
var item:MovieClip = MovieClip(event.target);
item.startDrag();
item.parent.addChild(item);
}
function releaseHandler(event:MouseEvent):void
{
var item:MovieClip = MovieClip(event.target);
item.stopDrag();
if (item.hitTestPoint(item.dropZone.x, item.dropZone.y))
{
item.x = item.dropZone.x;
item.y = item.dropZone.y;
item.buttonMode = false;
item.removeEventListener(MouseEvent.MOUSE_DOWN, dragHandler);
item.removeEventListener(MouseEvent.MOUSE_UP, releaseHandler);
matches++;
if (currentFrame < totalFrames && matches == totalMatches)
nextFrame();
}
else
{
item.x = item.originX;
item.y = item.originY;
}
}
game1();
[Frame 2]
function game2():void
{
matches = 0;
totalMatches = 0;
setPiece(board2.item1_mc, board2.dropZone1_mc);
setPiece(board2.item2_mc, board2.dropZone2_mc);
setPiece(board2.item3_mc, board2.dropZone3_mc);
setPiece(board2.item4_mc, board2.dropZone4_mc);
setPiece(board2.item5_mc, board2.dropZone5_mc);
setPiece(board2.item6_mc, board2.dropZone6_mc);
}
game2();
[Frame 3]
function game3():void
{
matches = 0;
totalMatches = 0;
setPiece(board3.item1_mc, board3.dropZone1_mc);
setPiece(board3.item2_mc, board3.dropZone2_mc);
setPiece(board3.item3_mc, board3.dropZone3_mc);
setPiece(board3.item4_mc, board3.dropZone4_mc);
setPiece(board3.item5_mc, board3.dropZone5_mc);
setPiece(board3.item6_mc, board3.dropZone6_mc);
}
game3();
Download / FLA / source: https://bit.ly/4d6EZ0a I hope this helps.
Regards,
JC
... View more