Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello developers,
I am currently making an android game for which i have a moving background. It works so far. But, i have another image and i want that image change every 10 seconds.
I have created a class called gamepanel, where most of the game work happens.
I have another class called background, and in that class i have created a constructor and passed the background image to the constructor in gamepanel class after creating an instance of background class in gamepanel.

and after that there is code for background class where i am drawing the first image and saying that if the image is off the screen then draw second image.
I want that second image comes after 10 seconds, until then, The first image should keep repeating.


Any help would be appreciated.

What I have tried:

This code is from gamepanel class.

public void surfaceCreated(SurfaceHolder holder){
 bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.field), BitmapFactory.decodeResource(getResources(), R.drawable.desert));
        bg.setVector(-5);


bg is instance of Background class.

Then, below is code of Background class.

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

public class Background {
    private MainThread thread;
    private Bitmap image,image_2;
    private int x, y, dx;

    public Background(Bitmap res, Bitmap res_2)
    {
        image = res;
        image_2 = res_2;
    }
    public void update() {
        x+=dx;
        if(x<-GamePanel.WIDTH){
            x=0;
            }

    }
    public void draw(Canvas canvas)
    {

        canvas.drawBitmap(image, x, y,null);
        if(x<0)
        {
            canvas.drawBitmap(image_2, x+GamePanel.WIDTH, y, null);
        }

    }
    public void setVector(int dx)
    {
        this.dx = dx;
    }
}
Posted
Comments
Nabeel Munir 1-Nov-18 1:39am    
One more thing, field is first image name and desert is second image name

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900