1

Android does not have main loop, but I have a ball in my arkanoid game test, and it needs to have the position changed every 100 ms.

I tried using MainThread library, but still it is not working.

If there was any possible way for me to override the function onRunning() and put it to check if the screen was touched and move the ball would be great.

But considering as I can't change the onRunning() function what could I do to add a function that will move my ball to the main game loop?

Ben
  • 51,770
  • 36
  • 127
  • 149

4 Answers4

1

You could create another thread which will run asynchronously with your Main/Gui thread

final boolean shouldRun = true;

Thread t = new Thread(){
    public void run(){
        while (shouldRun){
            //do what ever you need
            try{
                Thread.sleep(100); //sleep for 100ms
            } catch (Exception e) {

            }
         }
    }
 };

shouldRun = true;
t.start();

when you are done

shouldRun = false;
try { 
    t.join();
} catch (Exception e) { }

Note that this is not the most elegant solution, you should create a class which will handle the logic of the game on one thread and the drawing on another one.

Moreover, you should pause the logic game as well as the drawing whenever your activity is paused .

Kirill Kulakov
  • 10,035
  • 9
  • 50
  • 67
0

You just create a tread an repaint as you wish.

Here is an example: http://jmsliu.com/199/android-canvas-example.html

For basic animations, you do not need to use any library. If you want to develop a game, however, you should consider using a game engine.

Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
  • I liked this idea, so its like i establish a FPS and it will do like call 30 times onDraw() in a second? – Guilherme Garcia da Rosa Nov 04 '12 at 21:17
  • I didnt really understanded how I get that working, I already have a class that extends the view/surfaceview whatever, how can I set the amount of frames per second I want that to be draw? – Guilherme Garcia da Rosa Nov 04 '12 at 21:25
  • You can use System.currentTimeMillis() or something like that to measure elapsed time (delta = current - last) and sleep for (1000/FPS)-delta if delta is lower that 1000/FPS – J X Nov 04 '12 at 21:28
  • The linked example is a simple code example which does not care about FPS. If you want to introduce a sense of FPS, just use `Thread.sleep(1/FPS)` in the threads `run()` method. As I mentioned earlier, if you are doing something serious consider using a game engine. – Hakan Serce Nov 04 '12 at 21:29
  • I would certain use a game engine but is just a simple arkanoid for android, i have to deliever it tomorrow in my programming class, i have it done 85%, but i only got the ball to move when the screen is been touched, i wanted to make it moving automaticly, well gonna give a try to the example, gonna keep u posted – Guilherme Garcia da Rosa Nov 04 '12 at 21:31
0

As I know, android does not have main loop

Who said that? Rough example:

public class MainLoop extends Thread {
   boolean isRunning;

   public void terminate() {
    isRunning=false;
   }

   public void run() {
      isRunning=true;
      while (isRunning) {          
      //move ball
      try { sleep(100);
      ...

You also need to implement deltatime measuring and correct time-to-sleep value

J X
  • 55
  • 5
  • I guess for game development it would be more practical to extend SurfaceView and implement Runnable, since we can not extend more than one class. – Kirill Kulakov Nov 04 '12 at 21:17
  • I tried using MainThread library and stuff, but still it is not working. (as i said), well the thing is I cant get it to work, what should i set on MainActivity in other for that function to be called? – Guilherme Garcia da Rosa Nov 04 '12 at 21:19
  • Kirill, my example is from real working game, I have separate class for rendering which extends View. But putting logic and render into one class is also good solution, I agree. Guilherme, inside MainActivity you just create instance of your MainThread class and set listeners. – J X Nov 04 '12 at 21:25
  • Ok, but i'll set the listener to my view class, and how it will call the thread automaticly, there should be no such thing as do it by itself on programming lol – Guilherme Garcia da Rosa Nov 04 '12 at 21:27
  • Guilherme, you must .start() it, you can do it in onResume() for example – J X Nov 04 '12 at 21:33
  • Or if you're talking about passing events such as motion then you can implement listener inside MainActivity to pass event to your MainThread: ".setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View v, android.view.MotionEvent event){ return main_loop.onTouch(event);}}); ". Inside your MainThread class implement that listener: "public boolean onTouch(MotionEvent event) { posx = (int) event.getX(); // etc." – J X Nov 04 '12 at 21:36
0

You should use SurfaceView, which allows you to draw on a canvas from your own thread, in which you can control how often is rendered.

You may want to check this post for a discussion on the SurfaceView usage.

Also, if you want go directly to an example, check out this link

Community
  • 1
  • 1
andreban
  • 4,621
  • 1
  • 20
  • 49