wait random amount of secods then play

  • 13 years ago

    hi all

    I am in the middle of making a flash clay pigen shooting game and i need a bit of scripting to make the movie wait for a between 5 and 30 seconds before firing the the next clay.

    so as the movie loads i simply need it get to a fram and the script run and count for between 5 and 30 seconds and the continue playing onto the next frame and so conitune fireing the clays.

    i just can't et the timer and rand() funtions tro work correctly

    any eaxmples please??????

    cheers

    dean

  • 13 years ago

    Hi Dean, I'm new to Developer Fusion so it's some hours ago that I came to read your post. I would like to help you but I'm afraid that I will need a few details more. Sorry if you find some mistakes in my english, I'll appreciate a lot your pointing me any oversight.

    Anyway, I've taken some time to prepare two visually simple examples that use timers to control when the playhead of a certain MC jumps to the next frame once the delay variable value is reached. The delay values I used in my sample files (how could I send them or post them?) for each frame are:

    (In seconds) frame 1 (10), frame 2 (15), frame 3 (20), frame 4 (10) and frame 5 (5). You can try any value you want.

    1- Create a new flash document and drag 2 static texts onto layer 1. Write Elapsed Time: in one of them and Frame # 1, to display the number of the current frame,  in the other. They will serve as info labels.

    2- Drag a static text and change it into dynamic mode. Write txt_msg as the instance name for it.

    3- Create 5 keyframes. Be sure that the three previous design elements are present in each frame. Then select the static text with the words Frame #... in frame 2 and change the number 1 to 2, then in frame 3 change the 1 to 3 and so on, until you finish in frame 5 with Frame # 5. Alternatively you can use a Label component, asign it an instance name and change its text property using AScript but be sure to also create a TextFormat obj to asign the label a proper format while displaying the current frame dynamically.

    4- Create another layer to put the Action Script. Make 5 keyframes too.

    5- Select frame 1 and open the Script panel (F9). Now read what follows:

    FIRST APPROACH: writing the full code that sets up the timer in the first frame ( which I rarely do when the project is complex ). Here it goes. Play around with it to find new fresh ideas that fit your needs.

    THIS SCRIPT SHOULD BE PLACED IN THE FIRST FRAME OF THE MAIN TIMELIME

    ======================================================

    // The variables we need for setting up the timer

    /* the interval ID needed to control timer and which should be cleared after elapsed time reaches delay value otherwise the app  would crash. */

    var intervalId; 

    /* the delay time we need the playhead to stay in a certain frame.
    The value of this var can be different in other frames if needed. */

    var delay:Number = 10;

    // to capture the time when timer start in a frame

    var startTime:Number

    // the elapsed time, of course

    var elapsedTime:Number;

    /* Function that Updates the MClip by jumping to the next frame
    (or level). This function is assigned to the intervalID. See the
    las code line of the function beginInterval()*/

    function updateMC():Void {
     /* Uncomment if you want to check the Output panel while testing this example
     trace("updateMC intervalId: " + intervalId + " Elapsed Time: " + elapsedTime);
     trace("Start Time frame " + _currentframe + ": " + startTime + " seg.");
     trace("Elapsed Time frame " + _currentframe + ": " + elapsedTime + " seg.");
    */







     // here converting from miliseconds to seconds because I use seconds

     elapsedTime = int(getTimer() / 1000 - startTime);
     if (elapsedTime >= delay) {
      gotoAndStop(_currentframe + 1);
      clearInterval(intervalId);  // we clear the interval after moving to next frame
     } else {
     elapsedTime++; // we increment the elapsedTime count
     }
     txt_msg.text = elapsedTime;  // updating the dyanmic text
    }








    /* This is the function that we must call to start the timer. It checks
    first if there's a previous intervalId running and if so, then it is
    cleared, later it gets the new startTime and reassign the function updateMC()
    to the intervalId with the new delay passed value. */


    function beginInterval():Void {
     if (intervalId != null) {
      clearInterval(intervalId);
      trace("IntervalId " + intervalId + " has been successfuly cleared from memory.");
     }
     // we must convert from miliseconds to seconds if we're using seconds

     startTime = int(getTimer() / 1000);
     intervalId = setInterval(this, "updateMC", delay);
    }







    // calling beginInterval() to start timer

    beginInterval();

    stop();

     

    =====================================================

    IN THE OTHER FRAMES JUST PLACE THIS

    /* From this frame on, we only need to modify delay since the other
    variables with the functions are defined in first frame and the
    functions 'take care' of them. Alternatively, you can declare them
    as global variables. */


    var delay:Number = 15; // diferent values in the other frames as you need

    beginInterval();
    stop();


    ===================================================

    SECOND APPROACH: to write the functions code in an external file as follows,

    function updateMC():Void {
     // here converting from miliseconds to seconds because I use seconds

     elapsedTime = int(getTimer() / 1000 - startTime);
     if (elapsedTime >= delay) {
      gotoAndStop(_currentframe + 1);
      clearInterval(intervalId);  // we clear the interval after moving to next frame
     } else {
     elapsedTime++; // we increment the elapsedTime count
     }
     txt_msg.text = elapsedTime;  // updating the dyanmic text
    }








    function beginInterval():Void {
     if (intervalId != null) {
      clearInterval(intervalId);
      trace("IntervalId " + intervalId + " has been successfuly cleared from memory.");
     }
     // we must convert from miliseconds to seconds if we're using seconds

     startTime = int(getTimer() / 1000);
     intervalId = setInterval(this, "updateMC", delay);
    }






     

    then save the file as timer.as in a relative path to your .fla file for example, and include a reference to it using the #include directive as follow:

    // At the very beginning of the script in first frame of the tiemline write the following line of code

    #include "as/timer.as" // e.g. you store the timer.as file in the folder ' as '

    // then declare the variables

    var intervalId;
    var delay:Number = 10;
    var startTime:Number
    var elapsedTime:Number;


    // call the function beginInterval() to start the timer

    beginInterval();

    stop();

     

    For the rest of the frames, do the same as I explained at the end of FIRST APPROACH section.

    6- Save the .fla  and press Ctrl+Enter to Test the Movie. You should be directed to next frame each time the delay value for current frame is reached.

    I hope that this simple example could help you in some way to get your timers on.

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are 10 types of people in the world, those who can read binary, and those who can't.”