A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Movie That Plays Randomly

  1. #1
    Is it possible for a movie to play itself randomly. For example, an icon blinks, then maybe 3 seconds pass and it blinks, then 10, then 2...etc... Is this possible? How can I do it?
    Thanks.

  2. #2
    Senior Member
    Join Date
    Apr 2002
    Posts
    682
    This should give you the results you want. Click on the movie and add this code to it. You can tweak it to your liking:

    Code:
    onClipEvent(enterFrame) {
    myNumber=Math.round(Math.random()*50); 
    if (myNumber==3 && playing!=true) {_root.mc01.gotoAndPlay(2)}
    //reduce from 50 to shorten intervals between play
    
    }
    Replace mc01 with the instance name of your movieclip. Mc01 should start on frame 2, with a stop on frame 1. On frame 2, you should also have the following:

    Code:
    _global.playing=true
    then on the last frame of mc01, put:

    Code:
    _global.playing=false

  3. #3
    Thanks man.. A few questions, though.

    The "50" means that it could wait up to 50 seconds before it plays again? So if I wanted the most to be 20 I just replace that with 20?

    The instance is the name you give the symbol, right? ie. movie, graphic, button...whatever?

    Lastly, in MX is there a place where I could just copy and paste code or do I always have to go through the action pallette?

    Thanks for your help.

  4. #4
    Senior Member
    Join Date
    Apr 2002
    Posts
    682
    No. Here's what's happening with that code. Assuming that your movie is running at 12fps, every second a random number is being generated 12 times. I chose the number 50 because it seemed to randomly generate the number 3 in n-number of seconds, but never more than 10 seconds.

    Also, the above code assumes your mc is looping on stage. If not, you'll need to put this code into a constant loop. If you don't know how to do this I can show you.

    Instance name - yes, click on your movie clip and then in the properties box enter its instance name. This is very important.

    I never use the action pallette. Just set it to expert mode and you should be able to cut and paste.

  5. #5
    Cool. I'm going to try it now. Can you please tell me how to loop it, though? Thanks again, man.

  6. #6
    Senior Member
    Join Date
    Apr 2002
    Posts
    682
    Create a new movieclip (click Insert, then New Symbol, then Movie Clip). Drag this clip any place on stage. Give it the instance name of "loop" (or whatever).

    Place a keyframe on frame two of loop add to frame two:

    Code:
    _root.loop.gotoAndPlay(1)
    If you're going to do it this way, you'll need to change the code a little. Put the code onto frame one as:

    Code:
    this.onEnterFrame = function () {
    myNumber=Math.round(Math.random()*50);
    if (myNumber==3 && playing!=true) 
    {_root.mc01.gotoAndPlay(2)
    }
    }

  7. #7

    Sorry, man.

    Im sorry Kenny but now you totally lost me. First of all, in the movie clip, if I start the animation on frame 2 mthen when you initially open the main scene, you don't see the movie right away. Its a graphic that will randomly blink, but it needs to be there at all times. Am I misunderstanding? Are you talking about adding the code to the actual scene where the movie clip will be placed? In which case it would be on its own layer, but it still wouldnt be visible at first. Please help. Im relatively new at this, although I have a basic understanding.

  8. #8
    Senior Member
    Join Date
    Apr 2002
    Posts
    682
    If you can email or ftp to me your fla, I will have a better understanding of how your movie is setup.

    email to: kennybellewNOSPAM@kennybellew.com (remove the "NOSPAM" to email me)

    Or, ftp it to me at:

    Host: web.guardianhosting.net
    User: kenguest
    Password: kennybellew

  9. #9
    I emailed you.

  10. #10
    Senior Member
    Join Date
    Apr 2002
    Posts
    682
    When I try to open your file, I get "unexpected file format." Are you on a Mac? I've never been able to open mac fla's on my PC.

    To your original question, I said frame two because I assumed it was looping. You can change it to frame 1.

    If your _root timeline is not looping, then setup the loop as I mentioned previously.

  11. #11
    Yeah, I'm on a mac. That sucks. OK.

    "Create a new movieclip (click Insert, then New Symbol, then Movie Clip). Drag this clip any place on stage. Give it the instance name of "loop" (or whatever).

    Place a keyframe on frame two of loop add to frame two:



    code:
    ------------------------------------------------------------------------
    _root.loop.gotoAndPlay(1)


    ------------------------------------------------------------------------
    If you're going to do it this way, you'll need to change the code a little. Put the code onto frame one as:



    code:
    ------------------------------------------------------------------------
    this.onEnterFrame = function () {
    myNumber=Math.round(Math.random()*50);
    if (myNumber==3 && playing!=true)
    {_root.mc01.gotoAndPlay(2)
    }
    } "
    Is all this happpening in the actual movie that flashes, or in the scene where thta movie will be placed? That's my main question.



  12. #12
    Senior Member
    Join Date
    Apr 2002
    Posts
    682
    You create a new movie clip and drag it into the same timeline that contains the image you're wanting to randomly blink. This new mc will be named "loop". It's on the first two frames of the mc named "loop" that will contain all the code we've been discussing.

    Here's what I think you have in your fla. Tell me if I'm wrong.

    On the root timeline, you have separate movieclip (which has its own timeline) and this separate movieclip you want to randomly blink. Right?

    Here, try dl a sample file and see if you can open mine.

    http://www.kennybellew.com/misc/Random_play.fla

    In this file, I have a simple animation that plays when the movie loads. Then, it randomly plays from then on. Since it is the only mc on the _root timeline, the _root timeline is looping. So, I put the code right on the mc. Selct the mc and press F9 to see the code.

  13. #13
    Dude, I finally got it...thanks man. Can that be applied to the distance something moves? Object moves from point a to point b, then z, then c, then p...etc?

  14. #14
    Senior Member
    Join Date
    Apr 2002
    Posts
    682
    yep, I'm sure it can be adapted for that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center