Link to home
Start Free TrialLog in
Avatar of cu72
cu72

asked on

random jump to labelled set of frames

Hello

Could anyone help with this(simple)problem?

I want the playhead to run into a frame and randomly jump to a labelled frame further on in the timeline. There are 5 labelled frames to randomly choose from(labels are travel, portrait, still life, food, lifestyle. The idea is that each labelled frame loads in 2 external .swf files of images.

I have tried using this on the timeline jump frame:

myArray = new Array("travel" , "portrait", "still life", "food", "lifestyle");
randomNumber = Math.ceil(Math.random()*myArray.length);
gotoAndStop (myArray[randomNumber]);

its kind of working but not too well - makes me think I'm doing somthing wrong.

Any help would be greatly appreciated.

Thanks.


Level of Flash expertise: beginner/intermediate.


Avatar of henryww
henryww

what do u mean it is nothing too work?

ur code looks ok ... except
randomNumber = Math.ceil(Math.random()*myArray.length);

randomNumber = Math.floor(Math.random()*myArray.length);

the array is zero base :)

cheers
i mean it should be

randomNumber = Math.floor(Math.random()*myArray.length);

the array is zero base :)

cheers
maybe you should try

gotoAndStop (NUMBER(myArray[randomNumber]));
Avatar of cu72

ASKER

Thanks henryww that fixes one of the problems - but I have one more snag - and this might be something to do with my Flash inexperience..

This random action takes place in one scene (scene 2). When I try to put a scene before this (scene 1 - this scene contains some animation) the random action doesn't work any more. After going through scene 1 the playhead goes to scene 2 where the random jump frame is - but the playhead then always jumps straight to the last labelled frame in the scene - it seems to ignore most of the random action code.

there is nothing in scene 1 - just the usual 'gotoAndPlay ("Scene 2", 1); code.

Hope this makes sense.

Thanks



well, u mean u insert a new scene before that random function? ... and it doesn't work ...

try the following and make sure labels are different
rename the scenes so there is no space between scene & 1
ie: scene 1 -> scene1 or any single word no spaces

// put the code in scene1 - frame 1
myArray = new Array("travel" , "portrait", "still life", "food", "lifestyle");
function rndLabel(){
     r = Math.floor(Math.random()*myArray.length);
     gotoAndStop (myArray[r]);
}


// anywhere in sceneX - frame X
rndLabel();

see if this helps

cheers
Avatar of cu72

ASKER

Hello Henryww

Just to clarify

Scene1 has the animation

Scene2 has the random function and the labelled frames.

Scene2 with the code above works fine until I put in a Scene 1 before it.

I've taken out the spacing in scenes 1 + 2 - no difference.

Sorry - are you suggesting I put your script in scene 1 frame 1 and stick the rndLabel(); anywhere in scene 1?

I'll certainly try it if I have understood correctly.

Thanks again


ASKER CERTIFIED SOLUTION
Avatar of henryww
henryww

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cu72,
Try this it works for me..

myArray = new Array("travel", "portrait", "still life", "food", "lifestyle");
randomNumber = random(myArray.length);
trace (randomNumber+" = "+myArray[randomNumber]);
gotoAndStop (myArray[randomNumber]);

hope this help

rex
Avatar of cu72

ASKER

Hello henryww

Sorry - got tied up with something else - I've just tried what you suggested and it works fine - sorted out the problem. Thank very much for your help. I don't know exactly why putting the code at the beginning and calling the function later on worked - perhaps if you have a moment you could tell me why. Don't worry if you haven't the time and thanks again for the help.

cheers

thanks also rexmor for you suggestion - I'll have a look at this.
that's ok as long as ur problem is solved.

anyway, the putting the function is scene1 (first scene) works, i am not 100% sure why, guess that's the way scenes and labels are arranged and it goes back to the very beginning and start looking from there to see if there is a matching label ...

cheers