galileo79
galileo79

Reputation: 13

Android SurfaceView/Thread for update games states

I'm developing a small game on Android and I have a question. I want my main game loop to be the thread that is called from the SurfaceView but every time I want to call a method of another class from this thread I get the "stopped unexpectedly" message.

To be more clear:

@Override
public void run() { //Game Loop
   Canvas canvas = null;
   while (mRun) {               
      canvas = mHolder.lockCanvas();                
      if (canvas != null) {
         grub.updateGrubber();   //here I want to update state.                 
         mPanel.doDraw(canvas);
         mHolder.unlockCanvasAndPost(canvas);                   
      }
   }
}

the line grub.updateGrubber(); show the error that I mention before. I tried different methods from that class and all of them gives me the same error.

Any help will be appreciated.

Upvotes: 0

Views: 589

Answers (2)

galileo79
galileo79

Reputation: 13

I'm actually tested what you suggested and is not a bad idea, but the problem in my code was that I never really created a Grubber object (I miss the = new Grubber(); part).

Probably a mistake related with the enormous amount of code writing and testing that I've done in the past few weeks, sorry about that. Anyway I'm going to use as main game loop the thread that is created to update the canvas in the surfaceview according to what i've read here.

Upvotes: 1

Akhil
Akhil

Reputation: 14058

If my guess is correct, you are trying to update the UI thread directly. You can't call UI methods from threads other than the main thread. Try:

Runnable action = new Runnable() {  
            @Override 
            public void run() {    

                grub.updateGrubber();  
                }     };
                this.post(action);
 instead of your `grub.updateGrubber();`  line.

Upvotes: 1

Related Questions