Android游戏开发。异步任务。加载位图图像声音。

时间:2013-06-29 14:09:57

标签: android asynchronous android-asynctask task

我正在为Android开发此游戏。

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

基本上,正在发生的是,我正在加载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;
    }
}

现在我们V中的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);
                }

            }

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

0 个答案:

没有答案