0

I'm working on a card game app and i finished the basic stuff and now i'm trying to make it look professional. the first thing I want to do is the effect of the distribution of cards, i want to make a shuffle card effect. when a card is given to a player, I want at least 500 milliseconds difference to the next card that will be distributed to him. ideas? this is a part from my code..

    private void SetTheGame() {
    SetShuffleSound();
    for ( int i = 0; i < Imagename.length;i++) {
        Imagename[i] = (ImageView) findViewById(WTF[i]);
        CountCards();
        Random = getRandom();
        SwitchImages SwitchMe = new SwitchImages(myNewArray[Random]);
        int first = SwitchMe.ChangeImages();
        Imagename[i].setImageResource(myNewArray[Random]);
        Imagename[i].setVisibility(View.VISIBLE);
        CardsCount valueOfCard = new CardsCount(myNewArray[Random]);
        int a = valueOfCard.WhatsMyValue();
        String b = valueOfCard.TheFamily();
        switch (i) {
        case 0:
            if (first != 0) {
                Imagename[0].setImageResource(first);
            }
            FirstColumnComputer.add(a);
            FirstColumnComputerFAMILY.add(b);

            break;
        case 1:
            if (first != 0) {
                Imagename[1].setImageResource(first);
            }
            SecondColumnComputer.add(a);
            SecondColumnComputerFAMILY.add(b);

            break;
        case 2:
            if (first != 0) {
                Imagename[2].setImageResource(first);
            }
            ThirdColumnComputer.add(a);
            ThirdColumnComputerFAMILY.add(b);


            break;
        case 3:
            if (first != 0) {
                Imagename[3].setImageResource(first);
            }
            FourColumnComputer.add(a);
            FourColumnComputerFAMILY.add(b);

            break;
        case 4:
            if (first != 0) {
                Imagename[4].setImageResource(first);
            }
            FifthColumnComputer.add(a);
            FifthColumnComputerFAMILY.add(b);

            break;
        case 5:
            FirstColumnPlayer.add(a);
            FirstColumnPlayerFAMILY.add(b);

            break;
        case 6:
            SecondColumnPlayer.add(a);
            SecondColumnPlayerFAMILY.add(b);

            break;
        case 7:
            ThirdColumnPlayer.add(a);
            ThirdColumnPlayerFAMILY.add(b);

            break;
        case 8:
            FourColumnPlayer.add(a);
            FourColumnPlayerFAMILY.add(b);

            break;
        case 9:
            FifthColumnPlayer.add(a);
            FifthColumnPlayerFAMILY.add(b);

            break;

        }
        Cards.remove(Random);
  //        MakeTheCardPause();
    }
    SentTheLinkedList();
}  

MakeTheCardPause() is the problem...

       private void MakeTheCardPause() {
      Thread Timer = new Thread()
    {
        public void run()
        {
            try{
                sleep(1000);
            }catch(InterruptedException e)
            {
            e.printStackTrace();
            }finally
            {
            //do something...   
            }

        }
    };  
    Timer.start();
}

thanks!

digitalmidges
  • 826
  • 3
  • 13
  • 24
  • 1
    You should check out this previous SO question, which goes on to explain how to properly use sleep in the way you're attempting. http://stackoverflow.com/questions/14005549/how-to-properly-use-thread-sleep – Joseph Boyle Mar 14 '14 at 19:31
  • 1
    you dont need any Threads, just use a Handler and send delayed Messages to make your desired effect – pskink Mar 14 '14 at 19:41
  • Indeed, sleep is not the proper way to accomplish this on an event driven system such as Android - instead, you should be using one of the various mechanisms for scheduling a future event, and dealing the next card in that event. – Chris Stratton Mar 14 '14 at 19:45
  • @JoeBoyle - that is a generic Java answer suitable for creating minimum time between console output messages, but it is not a particularly appropriate solution for Android UI programming. – Chris Stratton Mar 14 '14 at 19:47
  • Can you create a Runnable and then call postDelayed() on it and pass it 500 millisecond? – Eenvincible Mar 14 '14 at 19:48
  • can you give me an example? how can i use Handler in this case? – digitalmidges Mar 14 '14 at 20:02
  • @digitalmidges extend a Handler, override its handleMessage, and read the docs about Handler.send[Empty]MessageDelayed – pskink Mar 14 '14 at 20:17

2 Answers2

2

Many ways you can do this. Thread.sleep(500) is the way was you suggested but it is not what I would recommend. Here are two alternatives

Message Handler

An example

Handler mHandler = new Handler(){           
            public void handleMessage(Message msg){
                super.handleMessage(msg);
                switch(msg.what){
                    case shuffle:
                        // Do something
                        break;
                    case doneShuffle:
                      //Do something
                }
            }       
        };

Asynch Tasks

Here is an example:

private class shuffleCards extends AsyncTask<Card, Integer, Long> {
     protected Long doInBackground(Card card) {
         //Do something
            //shuffle deck
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return deck;
     }

     protected void onProgressUpdate(Integer... progress) {
         //Number of shuffled cards??
     }

     protected void onPostExecute(Long result) {
        //Show card
     }
 }

Remember this is just a background task to display results. Your main thread will be handling the actual card values and handing them over to the Asynch task.

Good Luck

SeahawksRdaBest
  • 868
  • 5
  • 17
0

What about this? You need to have the sleep in the working thread, your code above is creating a new thread and telling it to sleep, which has no noticeable effect to the user.

private void SetTheGame() {
    SetShuffleSound();
    for ( int i = 0; i < Imagename.length;i++) {
        Imagename[i] = (ImageView) findViewById(WTF[i]);
        CountCards();
        Random = getRandom();
        SwitchImages SwitchMe = new SwitchImages(myNewArray[Random]);
        int first = SwitchMe.ChangeImages();
        Imagename[i].setImageResource(myNewArray[Random]);
        Imagename[i].setVisibility(View.VISIBLE);
        CardsCount valueOfCard = new CardsCount(myNewArray[Random]);
        int a = valueOfCard.WhatsMyValue();
        String b = valueOfCard.TheFamily();
        switch (i) {
        case 0:
            if (first != 0) {
                Imagename[0].setImageResource(first);
            }
            FirstColumnComputer.add(a);
            FirstColumnComputerFAMILY.add(b);

            break;
        case 1:
            if (first != 0) {
                Imagename[1].setImageResource(first);
            }
            SecondColumnComputer.add(a);
            SecondColumnComputerFAMILY.add(b);

            break;
        case 2:
            if (first != 0) {
                Imagename[2].setImageResource(first);
            }
            ThirdColumnComputer.add(a);
            ThirdColumnComputerFAMILY.add(b);


            break;
        case 3:
            if (first != 0) {
                Imagename[3].setImageResource(first);
            }
            FourColumnComputer.add(a);
            FourColumnComputerFAMILY.add(b);

            break;
        case 4:
            if (first != 0) {
                Imagename[4].setImageResource(first);
            }
            FifthColumnComputer.add(a);
            FifthColumnComputerFAMILY.add(b);

            break;
        case 5:
            FirstColumnPlayer.add(a);
            FirstColumnPlayerFAMILY.add(b);

            break;
        case 6:
            SecondColumnPlayer.add(a);
            SecondColumnPlayerFAMILY.add(b);

            break;
        case 7:
            ThirdColumnPlayer.add(a);
            ThirdColumnPlayerFAMILY.add(b);

            break;
        case 8:
            FourColumnPlayer.add(a);
            FourColumnPlayerFAMILY.add(b);

            break;
        case 9:
            FifthColumnPlayer.add(a);
            FifthColumnPlayerFAMILY.add(b);

            break;

        }
        Cards.remove(Random);
        long sleepMax = 1000L;
        Random r = new Random();
        long delay = (long) (r.nextDouble() * range);
        Thread.sleep(delay);
    }
    SentTheLinkedList();
}
AWT
  • 3,657
  • 5
  • 32
  • 60