无限的背景动画

时间:2016-04-22 04:59:31

标签: android

我为Android 2D游戏制作了无限背景,但背景只滚动了一次而且没有循环。我该如何解决这个问题?

另一个问题:如何让这个背景从上到下堕落?对于游戏

like this

这是我的班级(代码):

public class GameView extends View implements Runnable {
    private static final int INTERVAL = 10;
    private boolean running = true;
    private int y;
    private Paint paint;
    private int z = 0;
    int sx;
    Bitmap background;

    public GameView(Context context){
        super(context);
        paint = new Paint();
        background = BitmapFactory.decodeResource(getResources(), R.drawable.back);
        Thread myThread = new Thread(this);
        myThread.setPriority(Thread.MIN_PRIORITY);
        myThread.start();           
    }

    @Override
    public void run() {
        while (running) {
            try {
                Thread.sleep(INTERVAL);
            } catch (InterruptedException e) {
                Log.e("Game", "Gameloop finished!");
            }
            update();
        }
    }

    private void update() {
        if (y < getHeight()) {
            y += 5;
        } else {
            y = 0;
        }
        postInvalidate();
    }

    public void draw(Canvas canvas) {
        super.draw(canvas);
        z = z-10;
        if(z == -sx) {
            z = 0;
            canvas.drawBitmap(background, z, 0, null);
        } else {
            canvas.drawBitmap(background, z, 0, null);  
        }
    }

    public void release() {
        running = false;
    }
}

0 个答案:

没有答案