0

Я хотел бы инициализировать все компоненты игрового движка с помощью AsyncTask. Может ли кто-нибудь вести меня, как это будет сделано?Как реализовать инициализацию с помощью AsyncTask

Я хотел бы что-то вроде этого: 1. Перед тем как код работает, установить заставку (.xml) 2. запустить код инициализации 3. После того как все сделано, запустить экран загрузки игры

Вот мой текущий код:

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

     // Display splash screen 
     if(this.splashScreen != null) { 
      // .xml 
      setContentView(this.splashScreen); 
     } 

     // Do all the initialization 

     // Acquire a wakeLock to prevent the phone from sleeping 
     PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); 
     wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame"); 

     // Setup all the Game Engine components 
     gameEngineLog = new WSLog("WSGameEngine"); 
     gameEngineLog.setLogType(this.gameEngineLogType); 
     gameLog = new WSLog(this.gameLogTAG); 
     gameLog.setLogType(this.gameLogType); 
     io = new FileIO(this, getAssets()); 
     audio = new Audio(this); 
     wsScreen = new WSScreen(this, this.screenResizeType, this.customTopYGap, this.customLeftXGap, this.gameScreenWidth, this.gameScreenHeight); 
     graphics = new Graphics(this, wsScreen.getGameScreen(), wsScreen.getGameScreenextended()); 
     renderView = new RenderView(this, wsScreen.getGameScreen(), wsScreen.getGameScreenextended(), FPS, maxFrameskippes); 
     input = new Input(this, renderView, logGameEngineInputLog); 
     networkSocket = new NetworkSocket(this); 

     this.gameEngineLog.d(classTAG, "Completed initialization"); 
     setContentView(renderView); 

     // Check that the developer has initialized the assets 
     if(this.assets == null) { 
      this.gameEngineLog.w(classTAG, "The assets for the game haven't been defined!"); 
     } 

     this.getNetworkSocket().useAnalytics(this.analyticsAppAPIKey); 
     this.getNetworkSocket().useServerCommunication(this.serverCommunicationAppID, this.serverCommunicationClientKey); 
     this.getNetworkSocket().useFacebookCommunicaiton(this.facebookCommunicationAppID); 

     // Check if server communication should be initialized 
     if(this.networkSocket.getUseOfServerCommunication() == true) { 
      this.networkSocket.getServerCommunication().initialize(this, this.networkSocket.getServerCommunicationAppID(), this.networkSocket.getServerCommunicationClientKey()); 
     } 

     // Check if facebook communication should be initialized 
     if(this.networkSocket.getUseFacebookCommunication() == true) { 
      this.networkSocket.getFacebookCommunication().initialize(this.networkSocket.getFacebookCommunicationAppID(), true); 
     } 

     // Start the Load screen 
     // Once all of this code has been executed, the class that extends this class calls "setScreen(new LoadScreen(this));" to set the LoadScreen, which 
     // loads all the assets of the game 

    } 
+0

ли вы имеете в виду что-то вроде этого? : http://blog.blundell-apps.com/tut-splashscreen-with-progress-bar/ – Blundell

ответ

0

Согласно документации asynctask класс предоставляет четыре способа, которые можно использовать для реализации вашей загрузки логики приложения:

onPreExecute(), вызываемый в потоке пользовательского интерфейса сразу после выполнения задачи . Этот шаг обычно используется для настройки задачи, например, , показывая индикатор выполнения в пользовательском интерфейсе.

doInBackground (Params ...), вызывается на фоне потока сразу после onPreExecute() завершает выполнение. Этот шаг - , используемый для выполнения фоновых вычислений, которые могут занять много времени.

onProgressUpdate (Progress ...), вызываемый в потоке пользовательского интерфейса после вызова для публикацииProgress (Progress ...). Этот метод используется для отображения любой формы в пользовательском интерфейсе, в то время как выполняется расчет фона .

onPostExecute (Результат), вызывается в потоке пользовательского интерфейса после фона Выполнение вычислений. Результатом вычисления фона является , переданный этому шагу в качестве параметра.

Следуя документации, вы можете запустить свою задачу async в своей основной деятельности. В методе onPreExecute вы можете отображать свой заставку.

В методе doInBackGround вставьте код, который выполняет всю загрузку. Если вы хотите предоставить пользователю некоторую информацию о состоянии загрузки, вы можете использовать внутри этого метода publishProgress для публикации одного или нескольких единиц прогресса. Эти значения публикуются в потоке пользовательского интерфейса на этапе onProgressUpdate (Progress ...).

Наконец, в методе onPostExecute вы можете удалить заставку и запустить экран загрузки игры.

также смотрите на примере this.

bye

0

Долгосрочный ответ, медведь со мной.

Вам нужно сделать несколько вещей, чтобы получить то, что вы хотите.

Сначала отделите свои действия вместо вызова setContentView несколько раз.

Затем настройте объект домена, который является вашим «Настройки игры», чтобы вы могли загрузить его, инициализируя все его поля.

Также есть объект конфигурации для загрузки ваших настроек.

В приведенном ниже примере я не загрузил все ваши поля, но надеюсь, что вы поняли суть.

Окончательный результат будет что-то вроде этого:

Первая активность:

package com.blundell.tut.ui; 

import com.blundell.tut.R; 
import com.blundell.tut.domain.Config; 
import com.blundell.tut.task.LoadingTask; 
import com.blundell.tut.task.LoadingTask.LoadingTaskFinishedListener; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.ProgressBar; 

public class SplashActivity extends Activity implements LoadingTaskFinishedListener { 

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

     // Show the splash screen 
     setContentView(R.layout.activity_splash); 
     // Find the progress bar 
     ProgressBar progressBar = (ProgressBar) findViewById(R.id.activity_splash_progress_bar); 
     // Start your loading 
     new LoadingTask(progressBar, this, getAssets(), new Config()).execute("www.google.co.uk"); // Pass in whatever you need a url is just an example we don't use it in this tutorial 
    } 

    // This is the callback for when your async task has finished 
    @Override 
    public void onTaskFinished(GameDomain result) { 
      // The result object is your loaded files! 
     completeSplash(); 
    } 

    private void completeSplash(){ 
     startApp(); 
     finish(); // Don't forget to finish this Splash Activity so the user can't return to it! 
    } 

    private void startApp() { 
     Intent intent = new Intent(SplashActivity.this, MainActivity.class); 
     startActivity(intent); 
    } 
} 

Тогда AsyncTask, что делает загрузку:

package com.blundell.tut.task; 

import com.blundell.tut.domain.Config; 
import com.blundell.tut.domain.GameDomain; 

import android.content.res.AssetManager; 
import android.os.AsyncTask; 
import android.util.Log; 
import android.widget.ProgressBar; 

public class LoadingTask extends AsyncTask<String, Integer, GameDomain> { 

    public interface LoadingTaskFinishedListener { 
     void onTaskFinished(GameDomain domain); // If you want to pass something back to the listener add a param to this method 
    } 

    private static final int NUM_OF_TASKS = 2; 

    // This is the progress bar you want to update while the task is in progress 
    private final ProgressBar progressBar; 
    // This is the listener that will be told when this task is finished 
    private final LoadingTaskFinishedListener finishedListener; 

    private final AssetManager assets; 

    private final Config config; 

    /** 
    * A Loading task that will load some resources that are necessary for the app to start 
    * @param progressBar - the progress bar you want to update while the task is in progress 
    * @param finishedListener - the listener that will be told when this task is finished 
    */ 
    public LoadingTask(ProgressBar progressBar, LoadingTaskFinishedListener finishedListener, AssetManager assets, Config config) { 
     this.progressBar = progressBar; 
     this.finishedListener = finishedListener; 
     this.assets = assets; 
     this.config = config; 
    } 

    @Override 
    protected GameDomain doInBackground(String... params) { 
     GameDomain gameDomain = new GameDomain(); 
     Log.i("Tutorial", "Starting task with url: "+params[0]); 
     if(resourcesDontAlreadyExist()){ 
      downloadResources(gameDomain); 
     } 

     return gameDomain; 
    } 

    private boolean resourcesDontAlreadyExist() { 
     // Here you would query your app's internal state to see if this download had been performed before 
     // Perhaps once checked save this in a shared preference for speed of access next time 
     return true; // returning true so we show the splash every time 
    } 


    private void downloadResources(GameDomain gameDomain) { 
     // We are just imitating some process thats takes a bit of time (loading of resources/downloading) 
      int count = NUM_OF_TASKS; 

      // Do some long loading things 

      // Setup all the Game Engine components 
      gameDomain.setGameEngineLog("WSGameEngine", config.LOG_TYPE); 

      updateProgress(count--); 

      gameDomain.setAssets(assets); 

      updateProgress(count--); 
    } 

    public void updateProgress(int count) { 
     // Update the progress bar after every step 
     int progress = (int) ((NUM_OF_TASKS/(float) count) * 100); 
     publishProgress(progress); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 
     progressBar.setProgress(values[0]); // This is ran on the UI thread so it is ok to update our progress bar (a UI view) here 
    } 

    @Override 
    protected void onPostExecute(GameDomain result) { 
     super.onPostExecute(result); 
     finishedListener.onTaskFinished(result); // Tell whoever was listening we have finished 
    } 
} 

Тогда объект вы создание экземпляра:

public class GameDomain { 

    private WSLog gameEngineLog; 
    private FileIO io; 

    public void setGameEngineLog(String name, String logType){ 
     gameEngineLog = new WSLog(name); 
     gameEngineLog.setLogType(logType); 
    } 

    public void setAssets(AssetManager assets) { 
     io = new FileIO(assets); 
    } 

} 

, а также ваш Config:

package com.blundell.tut.domain; 

public class Config { 
    public String LOG_TYPE = "logType"; 
} 

Наконец ваш WakeLock должен быть «получил» в каждой деятельности OnCreate и выпустили в «OnPause»

+0

Спасибо! Я попробую! –

+0

Я не могу заставить его работать, как я этого хочу, я получил его для инициализации всего, но setContentView() не имеет никакого эффекта и по какой-то причине не отображает экран заставки. –

+0

Я разорвал это из своего сообщения в блоге здесь: http://blog.blundell-apps.com/tut-splashscreen-with-progress-bar/. Вы должны разделить свой заставку и свою основную деятельность на две отдельные операции, setContentView должен наглядно демонстрировать что-то – Blundell

Смежные вопросы