Copy link to clipboard
Copied
Hello,
I have been working on a application with actionscript 3. Its a simple interactive game which consits of answering a series of 5 questions. There is one line of code missing before its complete (its a matter of navigating through the game). I would very much appreciate some help on this!
First a "Home page" on which to commence; then comes the 5 questions: when a question is answered (right or wrong), the following question suddenly appears. Instead of this, I would like to include a "result page" after each question, depending on the response given by the player. For example, when a player would press "True", a page would follow saying "you have succeeded". When a player would press "False", a page would follow saying "you have failed". Then, in order to proceed to the next question there would simply be a "next question" button.
Here is the link to access the file:
https://drive.google.com/drive/folders/1c8PbQ9qGlBrfJ9yWhbZ2eeoRoRO-EBKc
Thanks in advance,
Karol
Hi.
Here is a suggestion:
AS3 code:
...import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
var step:uint = 5;
var time:uint = 5;
var tempTime:uint = time;
var timer:Timer;
var answers:Vector.<Boolean> = new <Boolean>[true, false, false, true, false];
var currentQuestion:uint = 0;
var lastFrame:uint = 25;
var loop:Boolean = false;
if (!this.started)
{
var lastQuestion:uint = step;
this.started = true;
}
function startTimer():void
{
tempTime = time;
setText();
timer
Copy link to clipboard
Copied
Hi.
Here is a suggestion:
AS3 code:
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
var step:uint = 5;
var time:uint = 5;
var tempTime:uint = time;
var timer:Timer;
var answers:Vector.<Boolean> = new <Boolean>[true, false, false, true, false];
var currentQuestion:uint = 0;
var lastFrame:uint = 25;
var loop:Boolean = false;
if (!this.started)
{
var lastQuestion:uint = step;
this.started = true;
}
function startTimer():void
{
tempTime = time;
setText();
timer = new Timer(1000, time);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
timer.start();
}
function stopTimer():void
{
timer.removeEventListener(TimerEvent.TIMER, timerHandler);
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
timer.stop();
}
function timerHandler(e:TimerEvent):void
{
tempTime--;
setText();
}
function timerCompleteHandler(e:TimerEvent):void
{
stopTimer();
lastQuestion = currentFrame;
gotoAndStop(1);
}
function setText():void
{
timerText.text = String(tempTime);
}
function checkAnswer(e:MouseEvent):void
{
var answer:Boolean = e.currentTarget.name.replace("Button", "") == "true";
currentQuestion = (currentFrame / step) - 1;
if (answer == answers[currentQuestion])
gotoAndStop("correct");
else
gotoAndStop("wrong");
stopTimer();
nextButton.addEventListener(MouseEvent.CLICK, nextQuestion);
}
function nextQuestion(e:MouseEvent):void
{
var targetFrame:uint = (currentQuestion + 2) * step;
if (loop)
{
if (currentQuestion % answers.length < answers.length - 1)
gotoAndStop(targetFrame);
else
gotoAndStop(step);
}
else
gotoAndStop(targetFrame);
if (currentLabel != "end")
{
startTimer();
trueButton.addEventListener(MouseEvent.CLICK, checkAnswer);
falseButton.addEventListener(MouseEvent.CLICK, checkAnswer);
}
else
{
restartButton.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
{
lastQuestion = step;
stopTimer();
gotoAndStop(1);
});
}
}
function start():void
{
stop();
startButton.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
{
gotoAndStop(lastQuestion);
startTimer();
trueButton.addEventListener(MouseEvent.CLICK, checkAnswer);
falseButton.addEventListener(MouseEvent.CLICK, checkAnswer);
});
}
start();
FLA download:
animate_cc_as3_quiz_game_2.zip - Google Drive
Please let me me know if you have any questions.
Regards,
JC
Copy link to clipboard
Copied
Great suggestion and thank you!
I should have specified... I'm looking to have every "result page" different from one an other. For example, the "result pages" associated to question 1 would be "correct answer 1" and "wrong answer 1". Then, the "result pages" associated to question 2 would be "correct answer 2" and "wrong answer 2". And so on..
This way, the "result pages" could provide the player with additional information to the specific question immediately answered.
I cannot thank you enough for your amazing help,
Karol
Copy link to clipboard
Copied
Nice! You're welcome!
No problem.
To accomplish this, all we need to do is to add frame labels for the correct and wrong answers like this:
correct0, correct1, correct2, and so on...
wrong0, wrong1, wrong2, and so on...
Then update this part of the code:
if (answer == answers[currentQuestion])
gotoAndStop("correct" + currentQuestion);
else
gotoAndStop("wrong" + currentQuestion);
And it should work.
I've updated the same link with a new version.
I hope this helps.
Regards,
JC
Copy link to clipboard
Copied
Hi JoãoCésar,
Your code definitely works!
If you have the time I'd have one last question for you:
Is there a way to adapt this code in order to remove the "END" page?
Utlimately, the "result page" of the 5th question would lead back to the first question by clicking the "next question" button. This way, a neverending loop would be created amidst the 5 questions, never neading to "resart the game".
Thank you very much,
Karol
Copy link to clipboard
Copied
Hi.
Awesome!
The code already does what you want.
Just set the variable loop on line 12 to true and you should be good to go.
var loop:Boolean = true;
Regards,
JC
Copy link to clipboard
Copied
You are a true angel fallen from the heavens
!!!
Copy link to clipboard
Copied
It's awesome that it helped you!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now