Solved

Dynamically creating a CASE/IF statment based on the length of an array

Posted on 2007-11-21
4
165 Views
Last Modified: 2010-04-03
I am creating a quiz based flash game. All the questions are stored in an array, same with the answers. For each question a function is run which loads a movieclip and inserts the question and answer. You can see this in the code below. The _global.questionCounter is simply a variable that counts which question the code is at. This is in frame 1 and 2 of a movie which loops based on the answer to the question.

//Questions
var questions = new Array();
questions[0] = "Here is the text for question 1";
questions[1] = "Here is the text for question 2";
questions[2] = "Here is the text for question 3";
questions[3] = "Here is the text for question 4";
questions[4] = "Here is the text for question 5";

//Answers
var answers = new Array();
answers[0] = "Blue";
answers[1] = "Red";
answers[2] = "Green";
answers[3] = "Yellow";
answers[4] = "Pink";


switch (_global.questionCounter) {
      case 0 :
        loadBox(answers[0], questions[0]);
        break;
      case 1 :
        loadBox(answers[1], questions[1]);
        break;
      case 2 :
        loadBox(answers[2], questions[2]);
        break;
      case 3 :
        loadBox(answers[3], questions[3]);
        break;
      default :
        trace("Done");
        break;
}

However this requires manual writing of the case statment. I want to know if there is someway of reproducing the same effect but automatically producing it from the array. ie: counting the number of records and the writing the relevant case statement. Is there someway of doing this?
0
Comment
Question by:alex_wareing
  • 3
4 Comments
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 20327464
Well, I'm not sure about creating the case statement dynamically, but you're absolutely right that your code as it stands is very labour intensive. Let me see if I understand correctly:

based on the _global.questionCounter variable (which will be an integer) you want to loadBox with the appropriate question and answer from the array, right?

So, this can (and really, really should) be written not as a case statement, but just by using the _global.questionCounter variable as your index to both arrays:

// --- replaces the entire switch block:
loadBox(answers[_global.questionCounter], questions[_global.questionCounter];
// -- end

Beautiful in its simplicity, eh? Instead of using a numerical literal (1, 2, 3 etc) in the function call as your array index eg: answers[1], just use the variable directly eg: answers[_global.questionCounter]

Let me know how this turns out.

Tom
0
 
LVL 14

Accepted Solution

by:
tomaugerdotcom earned 500 total points
ID: 20327471
WHOOPS - I'm missing a bracket. Man it's too early for this.

// --- replaces the entire switch block:
loadBox(answers[_global.questionCounter], questions[_global.questionCounter]);
// -- end
0
 

Author Comment

by:alex_wareing
ID: 20327562
Great works perfectly, i wrapped it with an IF statement to enable the number of questions to be based on the array:

if (_global.questionCounter < questions.length) {
      loadBox(answers[_global.questionCounter], questions[_global.questionCounter],_global.questionCounter+1);
} else {
      trace("Done");
}
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 20327786
Awesome. Glad to have helped!

T
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now