Eae
Eae

Reputation: 4321

Troubleshooting a delay on the UI thread

I'm calling this function from the onTouchEvent of my Android SurfaceView. The function pushGreenText is a custom function I wrote that pushes a string into an EditText that's part of my XML overlay for the SurfaceView. It looks to me like all of the work is being performed on the UI thread. There is a behavior that is strange to me I am seeing. For some reason I don't see the text as being pushed until after firstRollSP is done executing and out of scope. What I am trying to achieve is a situation where I see the text as pushed right away without the delay of waiting for the last curly brace of firstRollSP to finish. In all other places in code pushGreenText works instantly but from some reason in this function there is the delay.

static synchronized public void firstRollSP( ) throws InterruptedException {
                mGame.mDice.setDie1( mGame.mDiceFirstRoll.getDie1( ) );

                mGame.mDice.setRolled( true );

                MainActivity.activity.pushTextGreen( Strings.get_First_roll_X_Die1( ) );

                H.waitms( 2000 );


                mGame.mDice.setDie2( mGame.mDiceFirstRoll.getDie2( ) );

                while( mGame.mDice.getDie1( ) == mGame.mDice.getDie2( ) )
                {
                    Random mRng = new Random( );

                    int dProposedDie = mRng.nextInt( 6 ) + 1;

                    mGame.mDice.setDie2( dProposedDie );
                }

                MainActivity.activity.pushTextGreen( "Android rolled " + Integer.toString( mGame.mDice.getDie2( ) ) );

                H.waitms( 2000 );

                if( H.initWonFirstRoll( ) )
                {
                    MainActivity.activity.pushTextGreen( "Player won first roll." );

                    mGame.isFirstRoll = false;

                    mGame.isTurn = true;

                    mGameAI.isFirstRoll = false;

                    mGameAI.isTurn = false;

                    mGame.mDice.sort( );

                    mGame.mDice.setRolled( true );

                    mGame.mDice.setDiceAnimationComplete( true );

                    mGame.mOppDice.init( );
                }
                else
                {
                    MainActivity.activity.pushTextGreen( "Android won first roll." );

                    mGame.isFirstRoll = false;

                    mGame.isTurn = false;

                    mGameAI.isFirstRoll = false;

                    mGameAI.isTurn = true;

                    mGameAI.mDice.init( );

                    mGame.mOppDice.init( );

                    mGame.mDice.sort( );

                    mGameAI.mDice.setDie1( mGame.mDice.getDie1( ) );

                    mGameAI.mDice.setDie2( mGame.mDice.getDie2( ) );

                    mGame.mOppDice.setDie1( mGame.mDice.getDie1( ) );

                    mGame.mOppDice.setDie2( mGame.mDice.getDie2( ) );

                    H.waitms( 2000 );

                    mGameAI.mPossibleIndexes.calcPossibleTrianglesAI( );

                    if( mGameAI.mPossibleIndexes.anyPossibles( ) )
                    {
                        moveWholeTurnAI( );
                    }
                    else
                    {
                        H.endTurnAI( );
                    }
                }
            }

Upvotes: 0

Views: 32

Answers (1)

tiny sunlight
tiny sunlight

Reputation: 6251

static synchronized public void firstRollSP( ) throws InterruptedException {

    mGame.mDice.setDie1( mGame.mDiceFirstRoll.getDie1( ) );

    mGame.mDice.setRolled( true );

    MainActivity.activity.pushTextGreen( Strings.get_First_roll_X_Die1( ) );
    new Handler().postDelay(new Runnable(){
       //your code here.
    },2000 );

}

Upvotes: 1

Related Questions