Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Im working on this game for android.

And wanted to know if my thread architecture was right or wrong.

Basically, what is happening is, i am loading All the bitmaps,sounds etc in the initializevariables() method.

But sometimes the game crashes and sometimes it doesnt. So i decided to use async task. But that doesnt seem to work either (i too loads at times and crashes at times)

    @Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setFullScreen();
    initializeVariables();

    new initVariables().execute();

//  setContentView(ourV);
}

private void setFullScreen() 
{
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
}

private void initializeVariables() 
{
    ourV=new OurView(this);
    stats = getSharedPreferences(filename, 0);

    ballPic  = BitmapFactory.decodeResource(getResources(), R.drawable.ball5);
    platform = BitmapFactory.decodeResource(getResources(), R.drawable.platform3);
    gameB    = BitmapFactory.decodeResource(getResources(), R.drawable.game_back2);
    waves    = BitmapFactory.decodeResource(getResources(), R.drawable.waves);
    play     = BitmapFactory.decodeResource(getResources(), R.drawable.play_icon);
    pause    = BitmapFactory.decodeResource(getResources(), R.drawable.pause_icon);
    platform2 = BitmapFactory.decodeResource(getResources(), R.drawable.platform4);

    countdown = BitmapFactory.decodeResource(getResources(), R.drawable.countdown);

    bubbles      = BitmapFactory.decodeResource(getResources(), R.drawable.waves_bubbles);


    backgroundMusic = MediaPlayer.create(this, R.raw.music);


    jump  =  MediaPlayer.create(this, R.raw.jump);
    click =  MediaPlayer.create(this, R.raw.jump_crack);

    sm  = (SensorManager) getSystemService(SENSOR_SERVICE);
    acc = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    sm.registerListener(this, acc, SensorManager.SENSOR_DELAY_GAME);
    ourV.setOnTouchListener(this);

    dialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    dialog.setContentView(R.layout.pausescreen);
    dialog.hide();
    dialog.setOnDismissListener(this);

    resume = (Button) dialog.findViewById(R.id.bContinue);
    menu   = (Button) dialog.findViewById(R.id.bMainMenu);
    newTry = (Button) dialog.findViewById(R.id.bNewTry);

    tv_time = (TextView) dialog.findViewById(R.id.tv_time);
    tv_day  = (TextView) dialog.findViewById(R.id.tv_day);
    tv_date  = (TextView) dialog.findViewById(R.id.tv_date);

    resume.setOnClickListener(this);
    menu.setOnClickListener(this);
    newTry.setOnClickListener(this);

}

    @Override
protected void onResume() 
{
    //if its running the first time it goes in the brackets
    if(firstStart)
    {
        ourV.onResume();
        firstStart=false;
    }
}

Now what onResume in ourV does is , its responsible for starting the thread

            //this is ourV.onResume  
        public void onResume()
        {
            t=new Thread(this);
            isRunning=true;
            t.start();
        }

Now what I want is to initialise all bitmaps sounds etc in the async background method

    public class initVariables extends AsyncTask<Void, Integer, Void>
            {

                ProgressDialog pd;

                @Override
                protected void onPreExecute()
                {
                    pd = new ProgressDialog(GameActivity.this);

                    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    pd.setMax(100);
                    pd.show();
                }

                @Override
                protected Void doInBackground(Void... arg0) 
                {

                    synchronized (this) 
                    {



                    for(int i=0;i<20;i++)
                    {
                        publishProgress(5);

                        try {
                            Thread.sleep(89);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    }

                    return null;
                }

                @Override
                protected void onProgressUpdate(Integer... values)
                {
                    pd.incrementProgressBy(values[0]);
                }

                @Override
                protected void onPostExecute(Void result)
                {
                    pd.dismiss();

                    setContentView(ourV);
                }

            }

Now since I am new to this. You could tellme maybe if async is not required for such stuff and there is another way of doing it normally.

share|improve this question
add comment

1 Answer

You aren't doing any initialization within your AsyncTask. You basically just sleep and update a ProgressDialog. But what you should do, is to put most of the code from within your initializeVariables() method into your doInBackGround() method.

The way you are doing it, you are still using the main thread for all time-consuming tasks.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.