Skip to main content
Known Participant
August 29, 2006
Question

Randomly set visibility of Movie Clip

  • August 29, 2006
  • 2 replies
  • 252 views
I am trying to randomly set the visibility of a Movie Clip, and am missing something. I believe the problem lies in Frame 3, I am just not sure what it is. The file is three frames long; see code below. Any help is appreciated.

Thanks,
-Adam
This topic has been closed for replies.

2 replies

Known Participant
August 29, 2006
Thanks Craig and NSurveyor for your fast responses; it's all working beautifully now.

Also, thanks for the kind words on the site.

Take care,
Craig Grummitt
Inspiring
August 29, 2006
ok first it is not necessary to put the code on three frames. but let's go through your three frames:
////////frame 1
light01Result = 0 -----its not necessary to initialise this particular variable so let's get rid of that line
stop() ------ you probably don't want to stop here or we'll never get to frame 2 or three where the crux of your code is!

///////frame 2
light01Result = 1+Random(1) -----Random does not exist(be careful of upper/lower case problems) - there is a random() method which is deprecated since flash 5, but we'd be better off using the Math.random() method. this returns a decimal number between 0 and 1, so we round it to either 0 or 1 and add 1 to reach either 1 or 2. (see line in attached code)

////////frame 3
if (light01Result == 1) {
light01._visibility = false;
}
if (light01Result == 2) {
light01._visibility = false;
}
stop()

-------this section of code isn't too bad - just two main problems. First, there's no such thing as _visibility. there is, however, a movieClip/Button property called _visible. use this instead. Secondly, regardless of the result of your random variable, you're setting the visibility to false. surely you want to set one of them to true! And thirdly a minor issue is that you may as well put the word else before your second if statement as the two are mutually exclusive.

PS great site by the way!
August 29, 2006
A little bit more condensed:

light01._visible = Boolean(Math.round(Math.random()));