views:

264

answers:

3

hello All. I use to do this on AS2 code.

this.pause3Seconds = function () {
        trace(_global.something(_root.somthing));
        clearInterval(myInt);
    };
    var myInt = setInterval(this, "pause3Seconds", 3000);

Now trying to worked out this into a class.as file got all type of migration errors and warning.

So here I'm. anybody know how this can be done within a class.as AS3 way file ?

I'm not working from timelines. ( frames )

John

A: 
var startTime = getTimer();
while (true) 
{
   if (getTimer() - startTime >= sleepTime) 
   {
   //do something
   break;
   }
}

http://www.kirupa.com/forum/showthread.php?t=232714 - secocular's post

Allan
Thanks for your replied.Still I'm a bit confused, How does the player know how long we will be pausing The function ?john
johnsone
replace sleepTime with the pause time (3000). So basically the code stays in a loop until the time passed is greater than 3000 milliseconds, in whch case do whatever you need and then it breaks out of the loop. What are use wanting this for btw? Pausing the player like this should be avoided.
Allan
Locking the code in such a loop might lead to freezing of the swf and a busy cursor being shown. As Allan said, try not to use it. You might wanna consider some redesigning. Whats your usecase anyway?
Amarghosh
johnsone
+2  A: 

Your AS2 code doesn't actually pause the player (enterFrame listeners and mouse/key listeners will be executed during that three seconds). It just makes sure that the method pause3Seconds will be called three seconds later. You can achieve a similar functionality in AS3 using the Timer class.

var timer:Timer = new Timer(3000, 1);
timer.addEventListener(TimerEvent.TIMER, onTimerTick);
function onTimerTick(e:TimerEvent = null):void
{
    if(e)
    {
        trace("3 seconds completed");
        Timer(e.target).removeEventListener(TimerEvent.TIMER, onTimerTick);
    }
}
Amarghosh
yes I was wondering this, the name of the function led me to believe he wanted a sleep style pause but hopefully (from a coding point of view) this is what they want.
Allan
Thanks for your replied.you right,when using this AS2 way I'll always make sure nothing is on that frame, no buttons or what ever.this to force the player to wait till the 3 seconds has pasted.seams the best solution by then but not the right one.
johnsone
Got real close using this.Only problem is when the function is calling I get a Argument Error:Argument Error: Error #1063:Argument count mismatch on ontimertick (). Expected: 1, current value 0.any Idea ?
johnsone
You dont't have the required parameters in your function that is listening. Most likely missing e:TimerEvent
Allan
I've modified the method signature so that you can call it from other parts of the code without passing any parameters.
Amarghosh
Thanks for the Update Amarghosh..No error returns. But the ontimertick (); or this.ontimertick (); doesn't get trigger. when calling.
johnsone
Try removing the `private` attribute from the method signature. If it's still not working, post the code.
Amarghosh
Thank you very much Amarghosh. That did the trick.very much appreciated all the help here, and Thanks to all who has taken there time to replied to my thread.appreciated.
johnsone
Welcome! Removed private from the answer - am not much familiar with AS2
Amarghosh
You did very well here, was looking for this for a long time..Nice job.
johnsone
A: 

@Allan: That will make your flash code to throw runtime error (Script is taking longer than expected). It's always a bad idea to sleep inside a function.

@jon: This is somehwat like 'your way' solution :)

import flash.utils.*;//for setInterval and clearInterval
public class YourClass {
private var myInt:uint;
public function YourClass():void { //YourClass is the name of your class
myInt = setInterval(pause3Seconds, 3000);
}
public function pause3Seconds() {
trace("Whatever you want after 3 seconds");
clearInterval(myInt);
}
}

-bhups

bhups
Thanks for your replied Give this a try, and got the same error as above.Error #1063:Argument count mismatch on pause3Seconds(). Expected: 1, current value 0.at Function/http://adobe.com/AS3/2006/builtin::apply() at SetIntervalTimer/onTimer() at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick()any Idea ?Thanks john
johnsone
@bhups yeah I know hence my comment that it is bad :) Although 3 seconds may be within the script taking longer than expected - and you can change the time limit, aaand there is a way around the script error ;-)
Allan
the default script time limit is 15 seconds.
Amarghosh
package { import flash.utils.*;//for setInterval and clearInterval import flash.display.Sprite; public class YourClass extends Sprite{ private var myInt:uint; public function YourClass():void { //YourClass is the name of your class myInt = setInterval(pause3Seconds, 3000); } public function pause3Seconds() { trace("Whatever you want after 3 seconds"); clearInterval(myInt); } }}I've compiled this and is workin fine for me. i've used YourClass as Doucment class. Try this, it should work.
bhups
@Allan, @Amarghosh: Sleeping inside a function will surely defeat the idea of flash-runtime being event based, which only uses cpu when required and doesn't kill the browser. :)
bhups