Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How would I approach this?

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool How would I approach this?

    I'm making a game and it has a few endings and I have it so that when the endings are reached it is a game over. The thing is how would one approach in somehow delaying the game over by a few seconds so the person can at least read the ending I wrote out for them. Of course this is in Java, I wish it was in Python.......


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: How would I approach this?

    To make Java wait, you can use methods found in the Thread class.

    Particularly, you may want to examine Thread.wait().

    Util Timers and Swing Timers are other options, but I think the Thread methods are your best bet.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would I approach this?

    I'm a real novice when it comes to this.... I want to use Thread.wait() but how would I implement it?

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: How would I approach this?

    I apologize, the correct method is Thread.sleep().

    The sleep() method takes one parameter (a long). From the documentation I linked you to:
    sleep

    public static void sleep(long millis)
    throws InterruptedException
    Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.
    Parameters:
    millis - the length of time to sleep in milliseconds.
    So, how you use it would be to do
    Thread.sleep(long howLongToWait);
    Where the variable howLongToWait is a long, and tells the program how long to wait, in milliseconds.

    Be sure to really check out that documentation. Here it is again.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would I approach this?

    I did what you recommended and it seems good but I'm having a slight problem now:

    try {
        		new GameOver(ending1);
     
    	    	if (gameA == 2 && gameB == 0) {
    		    	g.setColor(Color.black);
    				g.drawString("You touched the body and it started", (x1 + 25), (y1 + 40));
    				g.drawString("to moan and it was a zombie!", (x1 + 25), (y1 + 60));
    				g.drawString("It bit you and now your one,", (x1 + 25), (y1 + 80));
    				g.drawString("great job", (x1 + 25), (y1 + 100));
    				g.setColor(Color.red);
    				g.drawString("THE END", (x1 + 100), (y1 + 120));
    				try {
    					Thread.sleep(10000);
    					ending1 = 1;
    					} //Tells me to add Finally right here, where the error message is.
    	    		}
    	    	}
    				catch (InterruptedException e) {
    				} 
     
        		catch (GameOverException1 e) {
        			g.setColor(Color.red);
        			g.drawString("NO PRESSING BUTTONS, GAME OVER", 60, 100);
        		}

    The other parts of the code are of no worries, they're good. The part that I want to sleep for a while is the action of ending1 = 1, which would initiate the exception class and cause the "NO PRESSING BUTTONS..." to come out but sadly I am probably doing something wrong, it won't run when I have it like this.

  6. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: How would I approach this?

    You may have to be more specific... Are you getting errors? Is there another problem? I don't have enough information to accurately help...

    What I can say is that you need to prepare everything you want to be made visible before you tell the Thread to sleep, because the Thread won't do anything while sleeping. If you use it in a while statement, the pause will occur with each iteration of the while.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. How would you approach this?
    By Staticity in forum Java Theory & Questions
    Replies: 3
    Last Post: October 10th, 2011, 12:09 AM
  2. How best to approach this project?
    By eb_dev in forum Java Theory & Questions
    Replies: 0
    Last Post: March 22nd, 2011, 11:43 AM