|
-
Random Frame method
Im wondering if anyone here knows how to make a Quiz type game play random frames but never the same frame twice.
I found an article on this at another place but it didn't work.
I know I have to make an array and splice, but thats all. I dont know how I would call the function when I need it (on the timeline).
Thanks for any help
Mark
-
self-portrait
Here's a quick example. Create an array with the desired frames
then use a function then splice it when you get a value. You could probably
populate (fill in) the array dynamically if the frames are spaced with evenly.
code:
frames = [1,3,6,8,23]; // this is the array of frames
getFrame ()
{
var i = random ( frames.length );
var j = frames [ i ];
frames.splice ( i, 1 );
return j;
}
// then use it like this
myClip.gotoAndStop ( getFrame () );
-
Senior Member
If all your frames wont be split up use this loop to add as many frames as you need to the array.
Code:
var FrameCount = 15;
function FrameCountArray() {
var i;
FrameArray = new Array();
for (i=0; i<FrameCount ; i++) {
FrameArray[i] = 0;
}
}
Im not shore if it will create the array if you dont declare it as anything, i havent tested it but i always just declared it as 0 to be on the safe side. Also then if i had to exclude that array slot from a loop or in your case a randomise function i'd set it to 1 and only exectuded code if the array was equal to 0.
Hope that helped
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
-
I cant seem to get this to work....
I used
frames = [1,3,6,8,23]; // this is the array of frames
getFrame ()
{
var i = random ( frames.length );
var j = frames [ i ];
frames.splice ( i, 1 );
return j;
}
// then use it like this
myClip.gotoAndStop ( getFrame () );
---------------------------------------------
by this "myClip.gotoAndStop ( getFrame ());"
Im not sure what this means? Im trying to make the _root timeline goto a random frame but if I do
_root.gotoAndStop ( getFrame () );
it doesnt do anything.
Any suggestions?
-
skylogic.ca
When dealing with arrays, you can also use the sort function istead of making your own with splice. Like this:
Code:
function arrayRandomizer(a, b) {
var value = (random(2)==0) ? -1 : 1;
return value;
}
frames = [1,3,6,8,23]; // this is the array of frames
frames.sort(arrayRandomizer);
currentFrameCount=0;
function nextFrame() {
gotoAndStop( frames[currentFrameCount] );
currentFrameCount++;
}
//....Some time later....
nextFrame();
Although the earlier examples given should work perfectly good too, and my example works in quite a similar way but I think that this way is a lot easier to use.
 Originally Posted by MarkSensei
Any suggestions?
Yes, fix the things Kianis forget (probably typed it up in a hurry eh?)
Code:
frames = [1,3,6,8,23]; // this is the array of frames
function getFrame ()
{
var i = random ( frames.length );
var j = frames [ i ];
frames.splice ( i, 1 );
return j;
}
// then use it like this
myClip.gotoAndStop( getFrame() ); //Extra spaces=bad (in some cases, depends where it is)
~MD
-
Senior Member
Hey thats not a half bad way, i had to make same sort of thing back when i didnt know much about arrays. Ill tell you how i did it and maby you'll get a laurgh out of it.
I created an array of the number of frames i wanted to randomise
I looped threw the slots and gave each 2 varibles, 1 was its number in the array and the second was to see if i had used it or not
I calulated how many frames i had left to go threw
I set a varible to create a random number up to how many i had left
I ran another loop to go threw the array, if it haddnt been used i took 1 of the random number and moved on, if the array number had been used i just moved on. Then finerly i would get to a new random frame.
All up about 70 lines of code and it probably wasnt the most easiest way to do it.
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
-
MD004, that worked like a charm, it was great cuz I already had a "_root.nextFrame();" on every frame so I didn't need to re-code anything. Thank you very much.
If its not too much trouble though, could you maybe explain a little about how that code is put together?
Thanks again
Mark
-
self-portrait
 Originally Posted by MD004
Yes, fix the things Kianis forget (probably typed it up in a hurry eh?
Ops, my bad :S Thanks MD004. Actually that's really bad - to leave out "function".
-
skylogic.ca
 Originally Posted by MarkSensei
If its not too much trouble though, could you maybe explain a little about how that code is put together?
I can certainly try.
The sort function takes a function for its parameter, this function you can call whatever you want. Sort then passes two arguments to your function, now if we were sorting alphabetically or chronologically or something like that than we would be using these 2 arguments (which are two values from the array to be compared) but we don't care about those (so i actually didn't need to have that 'a' and 'b' in arrayRandomizer(a, b) {).
Then our function has to tell the sorter what to do with the 2 values it gave us. Here's what sort is expecting:
• -1 if A appears before B in the sorted sequence
• 0 if A = B
• 1 if A appears after B in the sorted sequence
So I just randomly return a -1 or 1 by using random(2) which gives us 0 or 1, so if it == 0 then it changes to -1 else stay at 1.
There you have it...I better go study for exams now...
~MD
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|