Sign in to follow this  
Moose6912

Creating a delay in J2ME

Recommended Posts

I am programming a small game and i need to create a delay so that when my character collides with an enemy,everything freezes in it's tracks for a few second.I was thinking of using a delay method to set a delay of a few seconds before continue processing the other code etc etc.Can anyone suggest a delay method to use or another solution which will accomplish the desired effect.

Share this post


Link to post
Share on other sites
An exact answer depends on how you coded the game.
Do you have a game loop constantly running in the background? Or do you react to system events (key presses or Commands)?

In any case I think you should call System.currentTimeMillis() when the collision occurs, save the value, and check against it to see if enough time has passed.

shmoove

Share this post


Link to post
Share on other sites
Quote:
Original post by shmoove
An exact answer depends on how you coded the game.
Do you have a game loop constantly running in the background? Or do you react to system events (key presses or Commands)?

In any case I think you should call System.currentTimeMillis() when the collision occurs, save the value, and check against it to see if enough time has passed.

shmoove


Sorry for not being more clear earlier on.With regards to your question.Yes,I have a game loop that is constantly running in the background and i've made use of a thread to run the game application.

Share this post


Link to post
Share on other sites
Well, there are methods of pausing threads like Nuss said (mainly Thread.sleep() and Object.wait()), but I don't think you should be doing that. That will make the game unresponsive during the pause.
If you just save the time of collision in a variable and check that variable to see if the delay is over, you'll be free to still keep the game responsive to input and even do some animation that shows the collision (for example make the sprites flash or something).

A quick sketch would be something along the lines of:

int collisionTime = 0;
while (running) {
if (collisionTime != 0) {
// regular game code
if (collided) {
collisionTime = System.currentTimeMillis();
}
}
else {
if (System.currentTimeMillis() - collisionTime > PAUSE_TIME_IN_MILLIS) {
collisionTime = 0;
}
else {
// run in "collided" mode
}
}
}


shmoove

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this