アンドロイド用のこのゲームに取り組んでいます。
そして、私のスレッド アーキテクチャが正しいか間違っているかを知りたがっていました。
基本的に、何が起こっているかというと、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);
}
}
今、私はこれに慣れていないので。そのようなものに非同期が必要でなく、それを通常どおり行う別の方法があるかどうかを教えてください。