Skip to main content
Participant
April 24, 2014
Question

Flash Quiz Drag and drop help

  • April 24, 2014
  • 1 reply
  • 770 views

Hello, I have a Quiz where you drag a symbol over the correct answer, I have got it to work for a single question but I can't figure out how to get it to reset and work on the next frame with the new question.

Once the perosn chooses the correct answer a button apears allowing them to click onto the next question, if they get the wrong one then it shows the game over animation.In total I have 5 questions and after your get the final queston correct it shoudl go to a new frame where the winner animation plays. I'm not very good with scripting, sorry.

import flash.utils.Timer;

import flash.events.TimerEvent;

import flash.events.Event;

var myTimer:Timer = new Timer(1000,14)

var count:Number = 14

var startX:Number;

var startY:Number;

var counter:Number = 0;

stop();

nextbutton.visible=false;

Marker.buttonMode = true;

/*Timer*/

myTimer.addEventListener(TimerEvent.TIMER, countdown);

myTimer.start();

function countdown(event:TimerEvent):void {

txt_time.text = String((count)-myTimer.currentCount);

if(txt_time.text == "0"){

      gotoAndPlay("Game_Over")

}

}

/*Quiz Marker*/

Marker.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);

Marker.addEventListener(MouseEvent.MOUSE_UP, dropIt);

function pickUp(event:MouseEvent):void {

          event.target.startDrag(true);

          event.target.parent.addChild(event.target);

          startX = event.target.x;

          startY = event.target.y;

}

function dropIt(event:MouseEvent):void {

          event.target.stopDrag();

                    var myTargetName:String = "Correct" + event.target.name;

          var myTarget:DisplayObject = getChildByName(myTargetName);

          if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){

                    trace("testhere");

                    event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);

                    event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);

                    event.target.x = myTarget.x;

                    event.target.y = myTarget.y;

                    counter++;

          /*ANSWER WRONG*/

          } else {

                    myTimer.stop()

                    gotoAndPlay("Game_Over");

                    Marker.visible=false

                    event.target.x = startX;

                    event.target.y = startY;

          }

          /*ANSWER CORRECT*/

          if(counter == 1){

                                        myTimer.stop();

                                        nextbutton.visible=true;

                                        Cig_Timer.stop();

                                        event.target.x = startX;

                                        event.target.y = startY

    }

}

/*Next button*/

nextbutton.addEventListener(MouseEvent.CLICK, buttonClick_Quiz_Q2);

function buttonClick_Quiz_Q2(evt:MouseEvent):void {

     gotoAndStop("Quiz_Q2");

}

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
April 24, 2014

If each new question is in its own frame, as a minimum for the followup questions you need to assign the same event listeners for the new "Marker" objects on each new frame - I'd use different names for each.  Assigning it in the first frame does not assign it for all frames.  As long as the functions are generic in ature, which they appear to be, you can share them.

Participant
April 24, 2014

Ok it seems to have fixed the dragging section but the Cig_Timer movieclip dosnt stop and also the Next button dosnt show up. Also the timer is the same time as the previous question and is paused.

var Q2myTimer:Timer = new Timer(1000,14);

var Q2count:Number = 14;

var Q2startX:Number;

var Q2startY:Number;

var Q2counter:Number = 0;

stop();

nextbutton.visible = false;

Marker.buttonMode = true;

/*Timer*/

Q2myTimer.addEventListener(TimerEvent.TIMER, countdown);

Q2myTimer.reset();

Q2myTimer.start();

function Q2countdown(event:TimerEvent):void

{

          txt_time.text = String((Q2count)-myTimer.currentCount);

          if (txt_time.text == "0")

          {

                    gotoAndPlay("Game_Over");

          }

}

/*Quiz Marker*/

Marker.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);

Marker.addEventListener(MouseEvent.MOUSE_UP, dropIt);

function Q2pickUp(event:MouseEvent):void

{

          event.target.startDrag(true);

          event.target.parent.addChild(event.target);

          Q2startX = event.target.x;

          Q2startY = event.target.y;

}

function Q2dropIt(event:MouseEvent):void

{

          event.target.stopDrag();

          var myTargetName:String = "Correct" + event.target.name;

          var myTarget:DisplayObject = getChildByName(myTargetName);

          if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget)

          {

                    trace("testhere");

                    event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);

                    event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);

                    event.target.x = myTarget.x;

                    event.target.y = myTarget.y;

                    Q2counter++;

                    /*ANSWER WRONG*/

          }

          else

          {

                    Q2myTimer.stop();

                    gotoAndPlay("Game_Over");

                    Marker.visible = false;

                    event.target.x = Q2startX;

                    event.target.y = Q2startY;

          }

          /*ANSWER CORRECT*/

          if (Q2counter == 1)

          {

                    trace("correct");

                    Q2myTimer.stop();

                    nextbutton.visible = true;

                    Cig_Timer.stop();

                    event.target.x = Q2startX;

                    event.target.y = Q2startY

                    ;

          }

}

/*Next button*/

nextbutton.addEventListener(MouseEvent.CLICK, buttonClick_Quiz_Q3);

function buttonClick_Quiz_Q3(evt:MouseEvent):void

{

          gotoAndStop("Quiz_Q3");

}

Ned Murphy
Legend
April 24, 2014

You should not have to duplicate/rename all of the code in each frame.  The functions in the first frame can be used/shared for all of them as long as you extend that layer for the length of the question frames.

You can reuse the same Timer by reset()-ing it and start()-ing it again for each question.

You should start making use of the trace() function to help you troubleshoot your code.  You can use it to check the values of the different variables your code uses to see if there is a reason  something is not working as expected.  For instance, you say the next bbutton doesn't appear and the Cig_Timer movieclip doesn't stop.  I would suspect that because the

     if (Q2counter == 1)

is not satisfied when you think it should be.

So what you can do is trace the value of Q2counter ahead of that conditional to see

     trace(Q2counter);

     if (Q2counter == 1)