Skip to main content
Known Participant
April 11, 2019
Answered

Navigation inquiry w/ actionscript 3

  • April 11, 2019
  • 3 replies
  • 436 views

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

This topic has been closed for replies.
Correct answer JoãoCésar17023019

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

3 replies

Known Participant
May 7, 2019

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

JoãoCésar17023019
Community Expert
Community Expert
May 7, 2019

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

Known Participant
May 8, 2019

You are a true angel fallen from the heavens

!!!

Known Participant
April 12, 2019

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

JoãoCésar17023019
Community Expert
Community Expert
April 13, 2019

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

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
April 11, 2019

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