Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to write a delay function in ActionScript?
Index -> Graphics and Design, Web Design -> Flash MX Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HazySmoke)345




PostPosted: Sun Dec 21, 2008 2:24 pm   Post subject: How to write a delay function in ActionScript?

flash:
function f(){
    //lots of code here
    delay(1000);
    //lots of code here
}


I'm looking for a way to write a delay function that pauses the function for 1 second.

I tried to use an empty while loop that lasts 1 second, but the movie is frozen during that time.

I also tried to use 'setInterval', but that function doesn't do the trick. In my case, the portion of the code that's highlighted in red must not be executed until the delay is over. I could work around it if I try really, really hard, but there's probably a simple solution that I don't know about.

Does anyone know a way to write a delay function that really pauses the code, as if someone used a debugger at that point?


Mod Edit: Syntax tags were designed for formatting the code Smile
code:
[syntax="flash"]Code Here[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
jeffgreco13




PostPosted: Mon Dec 22, 2008 6:24 pm   Post subject: Re: How to write a delay function in ActionScript?

give me a bigger chunk of the code and more details on what you are trying to do... im guessing you're trying to delay tweens? based on ur last question..
HazySmoke)345




PostPosted: Mon Dec 22, 2008 6:53 pm   Post subject: Re: How to write a delay function in ActionScript?

Hi Jeff. It's nice to see you here again Very Happy

I'm trying to make a flash movie that animates Bubble Sort. There are 10 boxes on the flash movie. They're placed side by side so it look somewhat like an array. In addition to sorting an array of numbers, the flash movie makes it so that I can actually see the boxes moving around. Here's a simplified version of the code:

flash:
function sort(){
    var i:Number; var j:Number;
    for (i = 0; i < 10; ++i)
        for (j = i+1; j < 10; ++j)
            if (list[j] < list[j-1])
                swap(j, j-1);
}


The swap function, in this case, physically swaps the corresponding boxes in the movie. Thanks to your help last time, I can code the swap, but the swapping happens too slowly. While the first pair of boxes are being moved, the for-loops keep on running, and I just wish I could stop the code until the swapping is over.

I tried to google how to do the pause, and a lot of people suggest the SetInterval function. Of course, I can make the variables i and j global and change the values in the right order within the function, but that is a fundamental change to the bubble sort algorithm because it's kind of like turning the for-loops inside out. It would be better if ActionScript provides a clean way to pause the code.
zylum




PostPosted: Tue Dec 23, 2008 12:50 am   Post subject: RE:How to write a delay function in ActionScript?

In actionscript, the screen is not updated until all of the code is finished running thus a delay wont help you since you want an animation running during the delay. To make this work, you will need to save the state of the sort in between frames. When a swap is made, tween through the animation. When the animation is done, iterate to the next state.
jeffgreco13




PostPosted: Tue Dec 23, 2008 4:28 pm   Post subject: Re: How to write a delay function in ActionScript?

ok from what i understand the swap function isn't quick enough and the for loop continues running before the values are changed right?
HazySmoke)345




PostPosted: Tue Dec 23, 2008 8:56 pm   Post subject: Re: How to write a delay function in ActionScript?

Hi zylum and Jeff.

Jeff, you're right in a way. From my last question, each swap is supposed to last one second. Within the second, the boxes physically swap themselves, but the for loop doesn't care whether there's already tweening animation going on, and it just keeps going and tries to swap the next pair of numbers before the first pairs are in position.

zylum, thanks for answering the question. Smile If what you say is right, then I guess I have no choice but to break apart the sort function into something like what you said.

By the way, in case you haven't noticed, the bubble sort algorithm I wrote is wrong Sad The inner loop is supposed to iterate the other way, but you get the idea.
Display posts from previous:   
   Index -> Graphics and Design, Web Design -> Flash MX Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: