Solved

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

Posted on 2007-11-21
4
169 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
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.
Suggested Courses

734 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