2

我正在为安卓开发这款游戏。

并想知道我的线程架构是对还是错。

基本上,正在发生的事情是,我在 initializevariables() 方法中加载所有位图、声音等。

但有时游戏会崩溃,有时不会。所以我决定使用异步任务。但这似乎也不起作用(我有时也会加载并且有时会崩溃)

    @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;
    }
}

现在 ourV 中的 onResume 所做的是,它负责启动线程

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

现在我想要的是在异步后台方法中初始化所有位图声音等

    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);
                }

            }

现在因为我是新手。你可以告诉我这些东西是否不需要异步,并且还有另一种正常的方法。

4

0 回答 0