1

I'm trying to create a simple game loop (its not really a game yet) that displays a circle, then after it ticks 100 times draws another circle. I also have a text field that should display how many times the loop has ran. Relevant code is as follows:

MainActivity

public class MainActivity extends Activity
{
   @Override
   public void onCreate(Bundle savedInstanceState)
{
            super.onCreate(savedInstanceState);
    DrawView v = new DrawView(this);
    v.setBackgroundColor(Color.WHITE);
            setContentView(v);
     }
}

DrawView

public class DrawView extends SurfaceView implements SurfaceHolder.Callback
{
Paint p = new Paint();
MainThread thread;
private int y=0;

public DrawView(Context c)
{
    super(c);   
    thread = new MainThread(this, getHolder());
    thread.running = true;
    getHolder().addCallback(this);
    setFocusable(true);
}

public void draw(Canvas c)
{
    if(c==null)
        return;
    super.onDraw(c);
    p.setColor(Color.RED);
    p.setTextSize(32);
    p.setTypeface(Typeface.SANS_SERIF);
    c.drawCircle(getWidth()/2-100,getHeight()/2, 50, p);
    c.drawText("y = " + y, 50, 50, p);
    if(y==100)
        c.drawCircle(getWidth()/2+100,getHeight()/2, 50, p);
    else
        y++;
}

public void surfaceCreated(SurfaceHolder p1)
{
    thread.start();
}

MainThread

public class MainThread extends Thread
{
private DrawView page;
private SurfaceHolder holder;
public boolean running;

public MainThread(DrawView p, SurfaceHolder h)
{
    super();
    page = p;
    holder = h;
}

@Override
public void run()
{
    while(running)
    {
        Canvas c = holder.lockCanvas();
        page.draw(c);
        holder.unlockCanvasAndPost(c);
    }
}

}

It just displays the first circle and the text saying "y = 2." Nothing seems to update, or its doing it twice and then stopping. I am new to Android programming but not to Java. I'm sure I'm just missing something simple. Thanks for any help.

EDIT: Upon further observation, it seems the thread crashes randomly. Everytime I run the app, it dislays "y = " and then a different number each time. I'd reckon it makes it that many ticks before crashing. After I close the app, I get a message that says "Unfortunately, MyApp has stopped." I don't know enough about how Android works to know why its crashing.

EDIT 2: I've discovered its throwing an IllegalArgumentException on the line holder.unlockCanvasAndPost(c). Again, l'm not sure why. Can anyone explain what's happening and how to fix it?

EDIT 3: Logging the value of y each tick reveals that it couts up correctly and stops when it reaches 100 as intended. What happens onscreen does not reflect that for some reason.

  • Welcome to the Stack Exchange Family! I would like to point out that if you are asking about issues that a Game Developer may provide a better answer for, you can ask on the Game Dev SE site: http://gamedev.stackexchange.com/?as=1 Also, are you sure it is looping? try switching the `if (y==100)` to `if (y % 100 == 0)` this will test to see if y is a multiple of 100 instead of Only if it is 100. Chances are if there is an issue with your game halting it's probably on the 3rd or 4th iteration through the loop (where y == 2) which is why it stops there. Try running your game in Debugging mode. – sparks Oct 17 '13 at 15:23
  • That seems to what it is. I don't know why its stopping though. Since I'm writing this with AIDE on my tablet, I'm not sure if I have some sort of debug mode. Thanks for the tip about the game dev site too, I'm sure I'll be visiting there quite often :) – WillDaBeast509 Oct 17 '13 at 22:24

1 Answers1

0

I think what you're looking for is a combination of a Handler and a Runnable.

The handler calls the runnable, which runs once, then decides if it should call again.

something like:

    private Handler renderHandler = new Handler();
    private Runnable renderRunnable = new Runnable() {

        @Override
        public void run() {
            //... do stuff here;
            if(shouldRunAgain){
                renderHandler.postDelayed(renderRunnable, 1000); // for 1 second
            }
        }
    };

    renderHandler.post(renderRunnable);

edit: without seeing a log cat, I'm going to make an assumption that the UI updates are happening from a thread other than the main UI thread, causing the crash.

have a look at this, this, and this - I think these are more in line of what you're looking for.

Community
  • 1
  • 1
nebulae
  • 2,665
  • 1
  • 19
  • 16
  • That seems to be more of a workaround than a solution. I'd like to know why the game loop structure isn't working, because I plan to use that as a base for my apps. – WillDaBeast509 Oct 17 '13 at 22:25