I'm assuming that gameThread is something that has its own thread and
runs the game. And the main thread (on which onOptionsItemSelected is
running) is seperate and handles the user-interface.

If so, the it may be that createShapes() is pretty expensive and takes
a while. This'll hang the menu-option item until it's done and ready.

You could startup the game thread in yet another thread.
This other thread could be your gameThread. But i don't know its
interface and whether it has something that does except jobs/tasks,
e.g. gameThread.submitTask(new Runnable() {  public void run()
{ dosomething } }); Let's assume that it doesn't and this submitTask
on gameThread does not exist.

Then you have to create another thread to do this. I would use the
java.util.concurrent package for this:

in your activity you declare an instance variable:
  private ExecutorService mGameStarter;

in your onCreate of your activity:
  mGameStarter = Executors.newSingleThreadExecutor();

in your onDestroy:
  mGameStarter.shutdownNow();

And in your onOptionsItemSelected, inside your case START_GAME
  mGameStarter.submit(new Runnable() {
    public void run() {
      gameThread.doStart();
    }
  });


Every time you call submit() the new thread (hed by mGameStarter) will
wake up and execute the Runnable you specified when calling submit().
When the Runnable ends, this thread will sleep until you submit yet
another task.

On Mar 12, 9:48 pm, Ikon <ayanafr...@gmail.com> wrote:
> Streets,
>
> Here is the snippet where I do the menuHandling:
>
>     @Override
>     public boolean onOptionsItemSelected(MenuItem item) {
>
>         switch( item.getItemId()){
>         case START_GAME:
>                 gameThread.doStart();
>                 return true;
>         case STOP_GAME:
>                 gameThread.setState(gameThread.STATE_LOSE);
>             return true;
>         case PAUSE_GAME:
>                 gameThread.pause();
>             return true;
>         case RESUME_GAME:
>                 gameThread.unpause();
>             return true;
>         }
>
>         return super.onOptionsItemSelected(item);
>     }
>
> Here is my doStart method:
>
>     public void doStart() {
>         synchronized (mSurfaceHolder) {
>
>                 createShapes(mContext);
>                 shape[0].visible = true;
>                 score = 0;
>
>             mLastTime = System.currentTimeMillis() + 100;
>             setState(STATE_RUNNING);
>         }
>     }
>
> Thanks,
> Ayan
>
> On Mar 12, 10:28 am, Streets Of Boston <flyingdutc...@gmail.com>
> wrote:
>
>
>
> > Could you provide a code-snippet showing how you coded themenu-
> > handling and the starting of yourthread?
>
> > On Mar 12, 10:00 am, Ikon <ayanafr...@gmail.com> wrote:
>
> > > Hi,
>
> > > I am developing a game based on the SurfaceView/LunarLander example in
> > > the sample code.
> > > I have one activity, with onemenuoption that is "new game".
>
> > > My activity listens for themenuselection, and once new game is
> > > selected, doStart of the Gamethreadis called.  The game starts up
> > > after 3-4 seconds, but themenuoption sticks around on the screen
> > > until the game has started.  I think the mainthreadis waiting on the
> > > doStart to do its thing, but since it's creating a newthread, how do
> > > I get it to just start thethreadand continue its business of hiding
> > > themenuoption right away, instead of after 3 seconds?
>
> > > Thanks,
> > > Ayan- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to