Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

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.


0
cu72
Asked:
cu72
1 Solution
 
henrywwCommented:
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
0
 
henrywwCommented:
i mean it should be

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

the array is zero base :)

cheers
0
 
Saqib KhanSenior DeveloperCommented:
maybe you should try

gotoAndStop (NUMBER(myArray[randomNumber]));
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cu72Author Commented:
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



0
 
henrywwCommented:
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
0
 
cu72Author Commented:
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


0
 
henrywwCommented:
yes, try this

// 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]);
}


// scene1 last frame
gotoAndPlay("scene2",1);




then ... call the function in scene2
//scene2 - frame2
rndLabel();


see if that helps

:P
0
 
rexmorCommented:
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
0
 
cu72Author Commented:
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.
0
 
henrywwCommented:
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
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now