Skip to main content
Participant
July 19, 2013
Answered

How to make an ending when the last piece is completed

  • July 19, 2013
  • 1 reply
  • 494 views

Im new to flash

   Can someone tell me how to end a puzzle game where when the last piece of the puzzle matches/ collides with the last slot it will then go to a new movie/scene with a different backgrund and some text like "you win".  Below is the drag drop action code with no ending. I made a scene 2 and im planning it to go to scene 2(the one with new background and txt) once the puzzle is complete. I dont know how to do the code, i just copied this from an online tutorial which is great but no ending. Also at scene 2 i want to put a replay button where it goes back to scene 1(solve the puzzle thingy).  Im still new and dont have any proggramming background , any help is appreciated. Thankx.

piece1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStart1);
function dragStart1(event:MouseEvent): void {
piece1_mc.startDrag();
}

piece1_mc.addEventListener(MouseEvent.MOUSE_UP, dragStop1);
function dragStop1(event:MouseEvent): void {
piece1_mc.stopDrag();
if (piece1_mc.hitTestObject(holder1_mc)==true){
   piece1_mc.x=holder1_mc.x;
   piece1_mc.y=holder1_mc.y;
}
}

piece2_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStart2);
function dragStart2(event:MouseEvent): void {
piece2_mc.startDrag();
}

piece2_mc.addEventListener(MouseEvent.MOUSE_UP, dragStop2);
function dragStop2(event:MouseEvent): void {
piece2_mc.stopDrag();
if (piece2_mc.hitTestObject(holder2_mc)==true){
   piece2_mc.x=holder2_mc.x;
   piece2_mc.y=holder2_mc.y;
}
}

piece3_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStart3);
function dragStart3(event:MouseEvent): void{
piece3_mc.startDrag();
}

piece3_mc.addEventListener(MouseEvent.MOUSE_UP, dragStop3);
function dragStop3(event:MouseEvent): void{
piece3_mc.stopDrag();
  if (piece3_mc.hitTestObject(holder3_mc)==true) {
    piece3_mc.x=holder3_mc.x;
    piece3_mc.y=holder3_mc.y;
  }
}
piece4_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStart4);
function dragStart4(event:MouseEvent): void{
piece4_mc.startDrag();
}

piece4_mc.addEventListener(MouseEvent.MOUSE_UP, dragStop4);
function dragStop4(event:MouseEvent): void{
piece4_mc.stopDrag();
  if (piece4_mc.hitTestObject(holder4_mc)==true) {
    piece4_mc.x=holder4_mc.x;
    piece4_mc.y=holder4_mc.y;
  }
}
piece5_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStart5);
function dragStart5(event:MouseEvent): void{
piece5_mc.startDrag();
 
}

piece5_mc.addEventListener(MouseEvent.MOUSE_UP, dragStop5);
function dragStop5(event:MouseEvent): void {
piece5_mc.stopDrag();
  if(piece5_mc.hitTestObject(holder5_mc)==true) {
   piece5_mc.x=holder5_mc.x;
   piece5_mc.y=holder5_mc.y;
  }
}
piece6_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStart6);
function dragStart6(event:MouseEvent): void{
piece6_mc.startDrag();
}

piece6_mc.addEventListener(MouseEvent.MOUSE_UP, dragStop6);
function dragStop6(event:MouseEvent): void
{
piece6_mc.stopDrag();
if(piece6_mc.hitTestObject(holder6_mc)==true) {
   piece6_mc.x=holder6_mc.x;
   piece6_mc.y=holder6_mc.y;
}


This topic has been closed for replies.
Correct answer Ned Murphy

In each section where you test if there was a hit, when a hit does occur you should remove the event listeners to avoid allowing the object to be processed again, and then increment a counter so that you can check if all the pieces are accounted for as having been dropped.... example....

// outside all other code declare the variable

var plantedCount:int = 0;

// then inside you event handler functions do something like the following

if (piece1_mc.hitTestObject(holder1_mc)){

   piece1_mc.x=holder1_mc.x;

   piece1_mc.y=holder1_mc.y;

   piece1_mc.removeEventListener(MouseEvent.MOUSE_DOWN, dragStart1);

   piece1_mc.removeEventListener(MouseEvent.MOUSE_UP, dragStop1);

   plantedCount += 1;

   if(plantedCount == 6){

        gotoAndStop(1, "scene2");

   }

}

Note: When code repeats in patterns such as yours does it usually indicates the code can be condensed by making it more generic wherein the same functions are shared by each of the pieces rather than having sets of functions dedicated to each piece. 

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
July 19, 2013

In each section where you test if there was a hit, when a hit does occur you should remove the event listeners to avoid allowing the object to be processed again, and then increment a counter so that you can check if all the pieces are accounted for as having been dropped.... example....

// outside all other code declare the variable

var plantedCount:int = 0;

// then inside you event handler functions do something like the following

if (piece1_mc.hitTestObject(holder1_mc)){

   piece1_mc.x=holder1_mc.x;

   piece1_mc.y=holder1_mc.y;

   piece1_mc.removeEventListener(MouseEvent.MOUSE_DOWN, dragStart1);

   piece1_mc.removeEventListener(MouseEvent.MOUSE_UP, dragStop1);

   plantedCount += 1;

   if(plantedCount == 6){

        gotoAndStop(1, "scene2");

   }

}

Note: When code repeats in patterns such as yours does it usually indicates the code can be condensed by making it more generic wherein the same functions are shared by each of the pieces rather than having sets of functions dedicated to each piece. 

Participant
July 20, 2013

Thank You Sir, it worked well even if i am still learning and no knowledge yet i understand some things about the code, and my movie played to scene 2  It didnt work at first but i just put a stop(); code on my first scene. Ive been searching the net for codes that give me the easisiest explanation that can be understood by dummies like me and yours so far was the best. Now i just need to make a blinking text that keeps on blinking at scene 2, and a replay button.if the user wants to start the puzzle again. Can any1 help?