Copy link to clipboard
Copied
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");
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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");
}
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Ok the timer seems to work now but i cant drag the symbol on the second question. I guess I need to assign the same event listeners for the new "Marker" objects on each new frame like you mentioned before but i don't know how.
Copy link to clipboard
Copied
Assign the listeners the same way you assigned the listeners in frame 1, just do it in the other frames where the new draggable items are.
Copy link to clipboard
Copied
Ok well i added this
Marker.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
Marker.addEventListener(MouseEvent.MOUSE_UP, dropIt);
for Question 2 frame and now i can drag it but when i drag it over the correct answer it just sticks to it and dosnt pause the timer or anything.
I added the trace like you mentioned and rather than getting a 1 for the second question i get a 2. Guessing that the the coutner added 1 onto the previouse coutner so it was 2? So i tried changing the if statment from counter == 1 to counter <= 1 but that did'nt work.
Copy link to clipboard
Copied
If the value has to change at each different frame, then use a variable in place of the " 1 " and assign it the proper value for whatever frame you are in.
Pretend you name the variable: countVar
Then in frame 1 you would have:
var countVar:int = 1;
In frame 2, if it needs to be a 2, then assign it that value in frame 2:
countVar = 2;
Meanwhile back in frame 1, the conditional now looks like the following so that it can work for all frames (hopefully)...
if(counter == countVar)
Copy link to clipboard
Copied
Ok... it nearly works... question 2, 3 and 4 work but... for absoutly no reason question 5 wont let me pick the marker up, its exactly the same as the last 4 questions with the event listener names changed, i can;t figure out why it wouldnt work.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now