Results 1 to 6 of 6

Thread: Loop with pause

  1. #1
    mwadeiom is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Loop with pause

    Hi, I'm new to Java and really struggling with getting a simple card game to work so I wonder if anyone can help me.

    Imagine you have a shuffled deck of cards, then every second you flip a card over so you can see what it is. That's it!
    After that I'm going to compare the card to one the user picked but I think I can manage that part myself, the part I am stuck on is the one second pause in between cards, I'm sure it's something simple!


    The code below shows that I have a Deck class and a Card class, i draw a card from the deck, display it then try to pause for 1 second.
    The problem with this is that it just hangs until the 52 seconds are up and only displays the last card instead of a card every second.
    I've tried messing about with Timer but can't get that working either, can someone please point me in the right direction.

    P.S this is for an Android app but that part of it is already done, it's the Java I'm having trouble with.

    Thanks in advance!


    Java Code:
    	public void begin(View v) {
    
    		Deck deck = new Deck();
    		Card C;
    		TextView tv = (TextView) findViewById (R.id.currentCard);
            
    		
    		for (int i=0; i<=51; i++){
    			C = deck.drawFromDeck();
    			tv.setText (C.toString());
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			
    		}
    			
    		
    	}

  2. #2
    mwadeiom is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: Loop with pause

    That's what is there already but it just hangs and displays the last card when the time is up.

  3. #3
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: Loop with pause

    That should be working fine i dont see any problems with it...
    try putting your try/catch statement before it prints.. see if that changes anything

  4. #4
    mwadeiom is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: Loop with pause

    I finally got it working! Hopefully this helps anyone looking to do the same thing.
    This loops through the entire deck of cards, displaying one per second until the deck is gone, the timer is cancelled when there are no more cards left.

    Java Code:
    	public void begin(View v) {
    		
    		deck = new Deck();
            final TextView TV = (TextView) findViewById (R.id.currentCard);
    
            	final Timer timer=new Timer();
            	timer.schedule(new TimerTask(){
            		public void run(){
            			runOnUiThread(new Runnable()
            			{
            				@Override
            				public void run() {
            					Card C;
            					C = deck.drawFromDeck();
            					TV.setText(C.toString());
            					if (deck.getTotalCards() == 0){
            						timer.cancel();
            					}
            				}
            			});
            		}},0, 1000);
        		
    	}

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Loop with pause

    seems like a messy way to do it. But hey if it works then no problems, i see no reason why your original method didn't work, strange. perhaps you didn't implement runnable or something, hard to tell without the rest of your code.

  6. #6
    mwadeiom is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: Loop with pause

    Yeah I thought it seemed a little excessive for what I am trying to do. The original method just paused the system without displaying the cards each time, it ended after the 52 seconds was up.
    I'll have another look with the new stuff I've learned and see if I can get the code more efficient.

Similar Threads

  1. Pause ForEach Loop using Thread
    By The_Freeman in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-12-2013, 05:19 PM
  2. is there a way to pause java?
    By zhangster in forum New To Java
    Replies: 4
    Last Post: 04-24-2010, 08:38 PM
  3. How to pause
    By ravjot28 in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-31-2010, 07:09 AM
  4. Replies: 3
    Last Post: 06-06-2008, 12:10 AM
  5. Help with Pause
    By trill in forum Java Applets
    Replies: 2
    Last Post: 09-28-2007, 09:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •