dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: infinite loops

  1. #1
    Member
    Join Date
    Oct 2000
    Posts
    78
    what is the best way to create an infinite loop?

    right now i have 3 frames:

    frame 1 : initializes variables
    frame 2 : contains loop
    frame 3 : gotoAndPlay(2)

    what i would like to have happen is the movie ends up in the loop in frame 2 and never leave this loop.

    i have tried using "if" statements, so the stamen is never true. this causes more than 200,000 actions and disables the script.
    i have tried the "continue" command, but was unsuccessful. more than likely it was used incorrectly.
    i have tried a few other ugly scripting ways but all attempts have failed.

    does anyone know how to correctly use the continue command to have it jump back into the loop, or any other way without using 2 frames?

  2. #2
    Senior Member
    Join Date
    Dec 2000
    Posts
    181
    Maybe with the enterframe action??

    It could also be that flash has it limits

  3. #3
    Senior Member
    Join Date
    Dec 2000
    Posts
    248
    because of the way flash works youre just not allowed to make an infinite loop.

    the continue command is used when you are in a loop and dont want to execute the rest of the code in the loop, like in a collision function.

    // check objects for collision

    if(!colliding) continue;

    // resolve collision.

    so if they are not colliding it will jump back to the top of the loop.


    Depending on what you are doing in frame 2, one way to execute that code every frame is by using the onClipEvent(enterFrame) handler.

    Something i do often to give my code more of a procedural feel is to make a blank movie clip, put it in a layer on the main timeline, right click, goto Actions.

    then add :

    onClipEvent(enterFrame)
    {
    if(_root.running) _root.frameEvent();
    }

    then the code i want to run all the time is in a function called frameEvent. And i can stop or start the execution by changing the 'running' flag.

    that might be of no use to you at all...
    if it isnt, what are you doing that requires an infinite loop?

    martin.

  4. #4
    Member
    Join Date
    Oct 2000
    Posts
    78
    it cleared up a few things, thanks . i was using the continue command incorrectly, it was inserted into it's own line - my thinking was it would force it back into the loop, but now i see it must be used in conjunction with a statement checking if a certain event/value exists.

    what i was actually doing is dynamically controlling text - this is what i have in frame 2:
    -----------------------------------------------------------------------------------------------
    //set a variable, astart is set in frame 1
    ang = astart;
    // get mouse coords
    xmouse = getProperty(_root.drag, _x);
    ymouse = getProperty(_root.drag, _y);
    // checking if mouse coords are within a certain area
    if (xmouse>132 && xmouse<393 && ymouse>191 && ymouse<210) {
    //incrementing the radius in which the movie clip is able to move
    rad = rad+.9;
    aadd = aadd+.8;
    }
    /* getting the value of the radius the movie clip (c) is allowed to move
    the conver & rad values are created in frame 1*/
    xmove = Math.sin(ang*conver)*rad;
    setProperty ("_root.c", _x, 146.3+xmove); //the 146.3 offsets movie clip to center of screen
    /*incrementing the angle variable, allowing for a more random movement
    of each movie clip*/
    ang = ang+aadd;
    /* this script continues on repeating the last 3 lines for each movie
    clip i want to dynamically control*/
    -----------------------------------------------------------------------------------------------

    this is just a simple movie that dynamically controls text, it was a test to port over some code from a program called RT1. but in RT1, at the end of the loop you could tell it to jump back to the beginning of the loop. but i could not find a way to do this in flash.

    i understand everything you are saying except for the code contained within the clip event:
    -----------------------------------------------------------------------------------------------

    onClipEvent(enterFrame)
    {
    if(_root.running) _root.frameEvent();
    }

    -----------------------------------------------------------------------------------------------
    the _root.running - does this mean that as long as this instance exists on the main timline that the "if" statement will always be true? or is running a variable you set somewhere, and if so what is this statment really evaluating?


    the _root.frameEvent() - i have not really delved into functions yet but have noticed them in many .fla's i have downloaded. would you just simply create and a function and then have your loop in this function? and then every time you call upon the function - the script contained within it will run?

    sorry for the long post, but due to some poor documentation (not all of the documentation though) in the manual that comes with flash, i'm a still trying to grasp what all the commands and functions are, and there is tons of undocumented stuff which seem to be the most useful.

  5. #5
    Senior Member
    Join Date
    Dec 2000
    Posts
    248
    onClipEvent(enterFrame)
    {
    if(_root.running) _root.frameEvent();
    }

    ------------------------------------------------------------the _root.running - does this mean that as long as this instance exists on the main timline that the "if" statement will always be true? or is running a variable you set somewhere, and if so what is this statment really evaluating?
    running is a variable on the main timeline.

    writing :

    if(_root.running)

    is a short way of saying :

    if(_root.running == true)

    [/quote]
    the _root.frameEvent() - i have not really delved into functions yet but have noticed them in many .fla's i have downloaded. would you just simply create and a function and then have your loop in this function? and then every time you call upon the function - the script contained within it will run?
    [/quote]

    yeah, youve pretty much got it.

    Using a function is just a handy way of wrapping up code into a reusable block.

    for example :

    Code:
    function outputWord()
    {
       trace("word");
    }
    and to execute the code in the function (known as calling the function) you just write :

    outputWord();

    Now, using functions like this isnt greatly useful. To make them more useful you can do 2 things. Pass in variables and return variables to the caller.

    for example :

    Code:
    function addNumbers(a,b)
    {
       var result = a + b;
       return result;
    }
    to use (call) the function you'd write :

    addNumbers(4,5);

    but its not a lot of use if you dont get something from it. Thats what the 'return result' line is for. So when you call the function, you specify a variable to hold the return value :

    myResult = addNumbers(4,5);

    and myResult now holds the value 9.

    You can pass as many variables into the function as you want. You'll see these variables referred to as function parameters or function arguments. Both mean the same thing.

    The only limitation with a function is that you can only send one variable out at the end.

    The variables you send into a function or pass back out can be anything, movie clips, arrays, objects etc..

    So you can start to imagine the power you can get from programming this way.

    ----------------------

    The onClipEvent(enterFrame) is a function that is only applicable to movie clips.
    You'll often see it referred to as a 'handler' as well. But it does exactly the same thing as a function.
    The main difference is that you never call it yourself, it is called automatically by flash every frame.


    feel free to ask any more questions..

    martin.



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