5 Replies Latest reply on Apr 22, 2007 2:41 PM by kglad

# Easy question for all you ActionScript wizzkids!

I have created the following Actionscript to control a 'Next Question' button. The idea is that it will choose a random frame to link to in the current scene (Part2).

nextQ_btn.onRelease = function (){
gotoAndStop("Part2", n);
n = Math.random()*1+3;
}

At the moment, when I click the 'Next Question' button, nothing happens. What's up with my script? My programming knowledge is very limited and this is just about all that it can manage!

The button works when I take out the n variable and put in a frame number manually.

• ###### 1. Re: Easy question for all you ActionScript wizzkids!
monkee,

There appear to be two issues with your code:

1. you're calling n before you have defined it
2. you need to generate a random whole number and the random number generator will have decimals

The solution is

1. call for n after you define it
2. use a rounding action to make the random number a whole number

So:

nextQ_btn.onRelease = function (){

// generate a rounded random number between 0 and 4

n = Math.round(Math.random() * 4);
trace (n)

// go to the random number +1, making the range 1-5

gotoAndStop(n+1);
}

As you may already know, the trace action helps solve these types of issues by letting you know the value of n if it is defined... when you test movie, the results of the trace statement show n has a value in the output window. In your original code, it would show undefined.

Hope this helps. One other thing you may face is figuring out a way not to ask the same question twice. You probably will want to use an array of eligible questions before long.
• ###### 2. Re: Easy question for all you ActionScript wizzkids!
That's great, it works.
But you're right. I hadn't thought further than this. What I suppose I need to do is something which works out when all the questions have been displayed once, so that something different can happen at that point.

So, my guess is that I'll need to record that output from the trace and when the number of numbers in the output equals the total number of questions, then display an end page. Although I don't know where to begin doing that with actual Actionscript.

But you say I need an array to make sure each question only comes up once? How would I do that?
• ###### 3. Re: Easy question for all you ActionScript wizzkids!
because you're using scenes and the goto functions are prone to cause problems, you'll need to use frame labels for each frame that contains a question and use those labels in the _root.goto methods.

create an array of your frame labels, shuffle that array and you'll be able to present your questions in random order.

but first things first. label those frames and create an array of those labels.
• ###### 4. Re: Easy question for all you ActionScript wizzkids!
Once I've created an array and shuffled it, how do I relate this to the gotoAndStop link?

I came up with this, which is probably so far from right that it'll make you laugh!

quesionsArray = new Array("1", "2" , "3");
questionsArray.sort(function(a,b){return random(3)-1;});
questionsSubset = myArray.slice(0,3);

nextQ_btn.onRelease = function (){
gotoAndStop("Part2", questionsSubset);
}

And how would I specify what happens when all labels in the array have been used?
• ###### 5. Re: Easy question for all you ActionScript wizzkids!
don't use numbers to label your frames (and don't start object names with numbers) in flash. use something like: