Groups | Blog | Home
all groups > flash actionscript > april 2007 >

flash actionscript : Easy question for all you ActionScript wizzkids!


northern_monkee
4/21/2007 3:36:14 PM
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.


CodeManNew
4/21/2007 5:08:35 PM
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.
northern_monkee
4/22/2007 3:10:52 PM
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?
kglad
4/22/2007 5:06:32 PM
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.
northern_monkee
4/22/2007 9:31:29 PM
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?
kglad
4/22/2007 9:41:27 PM
don't use numbers to label your frames (and don't start object names with
numbers) in flash. use something like:



Array.prototype.shuffle = function() { // don't change anything in this method
for (var ivar = this.length-1; ivar>=0; ivar--) {
var p = random(ivar+1);
var t = this[ivar];
this[ivar] = this[p];
this[p] = t;
}
};

questionNum= ?; // use a number in place of the ?
questionsA=[];
for(var i=1;i<questionNum;i++){ // label your frames question1, question2 etc.
questionsA.push("question"+i);
}

questionsA.shuffle(); // your questions labels are randomized
ind=0;

nextBtn.onRelease=function(){
_root.gotoAndStop(questionsA[ind++]);
if(ind>=questionsA.length){
// do whatever. the last question has been reached.
}
}
AddThis Social Bookmark Button