Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

gotoAndPlay random frame array

New Here ,
Feb 17, 2010 Feb 17, 2010

Hi everyone,

I'm having some issues with AS3 ..... I have a slide show as a header for a website all of my pictures are set in the timeline with transitions. What i'm trying to do is have my slideshow goto a random frame onload to ensure a new look to the website on every visit. So here is what I have so far ...

// Array for specific random frames

var rdmFrame:Array = new Array();

rdmFrame[0] = "2";

rdmFrame[1] = "3";

rdmFrame[2] = "4";

rdmFrame[3] = "5";

rdmFrame[4] = "6";

rdmFrame[5] = "7";

rdmFrame[6] = "8";

rdmFrame[7] = "9";

rdmFrame[8] = "10";

rdmFrame[9] = "11";

// Random number equation

var randomNumber = Math.floor(Math.random()*10);

// Trace to show randomNumber and Array output

trace(randomNumber,rdmFrame[randomNumber]);

// gotoAndPlay Frame -- I have it set as gotoAndStop right now to see the trace and how its working

gotoAndStop(rdmFrame[randomNumber]);

My issue is everytime it loads it always works random but only goes up to frame #5 never over ???? any value over 6 it plays frame #5 ??? and I have no clue why? my trace is showing that my code is working right. I have attatched a demo its just a simple test to show you what i'm talking about. Thanks in advance

TOPICS
ActionScript
4.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 18, 2010 Feb 18, 2010

you don't need an array unless the possible start-frames have no easy-to-determine pattern.  in the two arrays you've shown, there's an easy-to-determine pattern so no array is needed.

but, if you have an array of start frames like:

var startframeA:Array = [5,6,33,43,55,111,123]; // with largest frame number at the final array index


// you could use:

stop();

var t:Timer=new Timer(100,0);

t.addEventListener(TimerEvent.TIMER,preloadF);

t.start();

function preloadF(e:TimerEvent){

if(this.framesLoaded>=star

...
Translate
Community Expert ,
Feb 17, 2010 Feb 17, 2010

you must wait (ie, use preloader code) until your 11 frames are loaded before executing your goto.

in addition, you can simplify your code considerably:

this.gotoAndStop(1+Math.ceil(Math.random()*10));

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 18, 2010 Feb 18, 2010

ok so like this?

var rdmFrame:Array = new Array();

rdmFrame[0] = "2";

rdmFrame[1] = "3";

rdmFrame[2] = "4";

rdmFrame[3] = "5";

rdmFrame[4] = "6";

rdmFrame[5] = "7";

rdmFrame[6] = "8";

rdmFrame[7] = "9";

rdmFrame[8] = "10";

rdmFrame[9] = "11";

this.gotoAndStop(1+Math.ceil(Math.random()*10));

or should i simplify further like this ...

var rdmFrame:Array = ["2","3","4","5","6","7","8","9","10","11"];

this.gotoAndStop(1+Math.ceil(Math.random()*10));

and sorry i'm fairly new what would be a good simple preloader code?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2010 Feb 18, 2010

no.  that one line of code replaces all your code (that you showed in this thread).  to add preloader code:

stop();

var t:Timer=new Timer(100,0);

t.addEventListener(TimerEvent.TIMER,preloadF);

t.start();

function preloadF(e:TimerEvent){

if(this.framesLoaded>=11){

t.stop();

t.removeEventListener(TimerEvent.TIMER,preloadF);

t=null;

this.gotoAndStop(1+Math.ceil(Math.random()*10));

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 18, 2010 Feb 18, 2010

ok awesome i now understand.

Just one thing .... the whole point of my slideshow is to load a random frame in a large movie so to load an array into that i would do something like this...

stop();
var t:Timer=new Timer(100,0);
t.addEventListener(TimerEvent.TIMER,preloadF);
t.start();
 
function preloadF(e:TimerEvent){
if(this.framesLoaded>=11){
t.stop();
t.removeEventListener(TimerEvent.TIMER,preloadF);
t=null;
var rdmFrame:Array = ["30","60","90","120","150","180","210","240","270","300"];
this.gotoAndStop(rdmFrame[1+Math.ceil(Math.random()*10)]); } }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2010 Feb 18, 2010
LATEST

you don't need an array unless the possible start-frames have no easy-to-determine pattern.  in the two arrays you've shown, there's an easy-to-determine pattern so no array is needed.

but, if you have an array of start frames like:

var startframeA:Array = [5,6,33,43,55,111,123]; // with largest frame number at the final array index


// you could use:

stop();

var t:Timer=new Timer(100,0);

t.addEventListener(TimerEvent.TIMER,preloadF);

t.start();

function preloadF(e:TimerEvent){

if(this.framesLoaded>=startframeA[startframeA.length-1]){

t.stop();

t.removeEventListener(TimerEvent.TIMER,preloadF);

t=null;

this.gotoAndStop(startframeA[Math.floor(Math.random()*startframeA.length)]);

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines