ForumsProgramming ForumHow to make a delay for 3 seconds?

11 8702
Seth23KvK
offline
Seth23KvK
15 posts
Nomad

I hate having to do this so much, having to ask questions to solve the problem without anywhere to start.
Anyway, I have a movieclip set to visible = false. (not the exact code) I want it to appear when triggered by a hitTest to appear for 3 seconds. Does anyone have any idea?

  • 11 Replies
3Aegis3
offline
3Aegis3
6 posts
Nomad

Have a number variable for the timer. For every frame make the timer one less of its value. (timer -= 1) When the object is 'hit' set the timer to 3 times your frame rate. (So if your frame rate is 30, your timer would be set to 90) Inside the same hit test, make your object visible. Also, have another piece of code checking if the timer equals 0, and when it does, make the object invisible. The timer will continue to count down, but that does not matter, as it will not hit 0 again until it is reset back to 90 (Or whatever you use) until it is hit again.

Seth23KvK
offline
Seth23KvK
15 posts
Nomad

Well that would work for 'this' instance but I plan on using a timer where that could not work. Thanks though.

master565
offline
master565
4,107 posts
Nomad

You could use an imitation timer. By that i mean, have a variable number, and have an enterframe event that starts when the hit test is set off, and makes the number increase by one every frame (also, lets assume your project is 10 fps). Then have it check every frame if the number is equal to thirty (i.e. 3 seconds)

var myNumber:number = 0;

addEventListener(Event.ENTER_FRAME, myEvent)

function myEvent(e:Event)
{
myNumber += 1;
if(myNumber >= 30)
{
visible = false;
myNumber = 0;
removeEventListener(yourevent)
}

(note: this code doesn't make the object visible, you need to make a separate function for that)

After the hit test, your number starts at 0 and increases every frame. After 30 frames (3 seconds) your number will be at 30, and when this happens it sets off functions in the if() statement. This function makes it turn back to invisible, resets the number variable, and stops the event.

(another note: i didn't check if this code actually works, so don't use it as reference. It was just an example and you will hopefully get the idea.)

Seth23KvK
offline
Seth23KvK
15 posts
Nomad

Well this was just like what Aegis said, I need it to be an actual timer if possible. I want this to exist in one frame because this is a game I am developing, and I do not want to have 30 frames that add to the filesize.

master565
offline
master565
4,107 posts
Nomad

Well this was just like what Aegis said, I need it to be an actual timer if possible. I want this to exist in one frame because this is a game I am developing, and I do not want to have 30 frames that add to the filesize.


The difference is that his was an actual time event, not an imitation of one. It sounds like you just don't want the time something without actually timing it. You could try to use the stop() function. The frames on the timeline won't move but the frames it is using to count will.
WhiskeyedJack
offline
WhiskeyedJack
80 posts
Shepherd

Depends on which version of ActionScript you are using. Off the bat, you want to set the clip to visible after the hitTest.

If you are looking for AS3 it is a little more straightforward. You want to create a Timer object. When you call getTimer,

// AS3
import flash.utils.*;

var hitTimer:Timer = new Timer(3000, 1); // 3 seconds = 3000 miliseconds, run 1 time
hitTimer.addEventListener(âtimerâ, afterHitTest);

function afterHitTest(eventArgs:TimerEvent)
{
//movieclip set to visible = false
}

Inside of your function with the hitTest you need to include:
hitTimer.start();
, after you set the movieclip to visible. When the timer runs out, it will call the afterHitTest() function and do whatever code you put in there.





milliseconds that have elapsed since Flash Player was initialized
WhiskeyedJack
offline
WhiskeyedJack
80 posts
Shepherd

Ignore the very last sentence fragment (How I wish we could preview/edit our posts). Also, for some reason, the forum did not want to display the quotes in that last post. Here is what the code should be.

// AS3
import flash.utils.*;

var hitTimer:Timer = new Timer(3000, 1); // 3 seconds = 3000 miliseconds, run 1 time
hitTimer.addEventListener("timer", afterHitTest);

function afterHitTest(eventArgs:TimerEvent)
{
//movieclip set to visible = false
}
WhiskeyedJack
offline
WhiskeyedJack
80 posts
Shepherd

Strike what I said about AS3 being more straightforward, fore some reason I though the setTimeout and setInterval functions were more complicated. For this, you want setTimeout.

// AS2
var timerInt:Number = setTimeout(afterHitTest, 3000); // 3 seconds

function afterHitTest(){
//movieclip set to visible = false
}


Just make sure you initialize the variable timerInt (and thus setTimeout) inside the function with the hit test, after you the clip to visible.
Seth23KvK
offline
Seth23KvK
15 posts
Nomad

Could you explain the Int:Number? And yes, I am doing this in As2.

WhiskeyedJack
offline
WhiskeyedJack
80 posts
Shepherd

In that line you are defining a variable. Basically we are saying that timerInt is a Number (we can actually call timerInt anything we want, it's our variable).

Unlike in AS3, in AS2 it is not required that you predefine the variable type. So if it is too bothersome or too confusing, leave it out.

So step by step:
* define a variable using var followed by the variable name (in this case timerInt)
* set the variable using the setTimeout function
* set the two arguments of the setTimeout function:
- function to execute after the timeout [in this case afterHitTest()]
- number of milliseconds to delay (in this case 3000 for 3 seconds)

Then all you have to do is define the afterHitTest() function somewhere in your code.

yamatryanangelo
offline
yamatryanangelo
1 posts
Nomad

can you tell me the exact code please sir??

Showing 1-11 of 11