How do I make this rendering thread run together with the main one?

Posted by funk on Game Development See other posts from Game Development or by funk
Published on 2014-06-13T07:37:52Z Indexed on 2014/06/13 9:44 UTC
Read the original article Hit count: 308

Filed under:
|
|

I'm developing an Android game and need to show an animation of an exploding bomb. It's a spritesheet with 1 row and 13 different images. Each image should be displayed in sequence, 200 ms apart.

exploding bomb spritesheet

There is one Thread running for the entire game:

package com.android.testgame;

import android.graphics.Canvas;

public class GameLoopThread extends Thread
{

    static final long FPS = 10; // 10 Frames per Second

    private final GameView view;

    private boolean running = false;



    public GameLoopThread(GameView view) {

        this.view = view;

    }



    public void setRunning(boolean run) {

        running = run;

    }



    @Override

    public void run() {

        long ticksPS = 1000 / FPS;

        long startTime;

        long sleepTime;

        while (running) {

            Canvas c = null;

            startTime = System.currentTimeMillis();

            try {

                c = view.getHolder().lockCanvas();

                synchronized (view.getHolder()) {

                    view.onDraw(c);

                }

            } finally {

                if (c != null) {

                    view.getHolder().unlockCanvasAndPost(c);

                }

            }

            sleepTime = ticksPS - (System.currentTimeMillis() - startTime);

            try {

                if (sleepTime > 0)
                {
                    sleep(sleepTime);
                } else
                {
                    sleep(10);
                }

            } catch (Exception e) {}

        }

    }
}

As far as I know I would have to create a second Thread for the bomb.

package com.android.testgame;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Bomb
{
    private final Bitmap bmp;

    private final int width;

    private final int height;

    private int currentFrame = 0;

    private static final int BMPROWS = 1;

    private static final int BMPCOLUMNS = 13;

    private int x = 0;

    private int y = 0;

    public Bomb(GameView gameView, Bitmap bmp)
    {
        this.width = bmp.getWidth() / BMPCOLUMNS;
        this.height = bmp.getHeight() / BMPROWS;
        this.bmp = bmp;
        x = 250;
        y = 250;
    }

    private void update()
    {
        currentFrame++;
        new BombThread().start();
    }

    public void onDraw(Canvas canvas)
    {

        update();

        int srcX = currentFrame * width;

        int srcY = height;

        Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);

        Rect dst = new Rect(x, y, x + width, y + height);

        canvas.drawBitmap(bmp, src, dst, null);
    }

    class BombThread extends Thread
    {
        @Override
        public void run()
        {
            try
            {
                sleep(200);
            } catch(InterruptedException e){ }
        }
    }
}

The Threads would then have to run simultaneously. How do I do this?

© Game Development or respective owner

Related posts about java

Related posts about android