Solved

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

Posted on 2007-11-21
4
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

627 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