Create a constant that loads several images and shows them on the screen

1

I'm developing a game and it's only generating an image on the screen randomly. I would like to implement 3 different images, which through a random would appear automatically on the screen, and that each image would have a value.

Example:

imagem1 = 1;
imagem2 = 2;

Imagine the game fruit ninja !! Mine is similar, except that in the place of fruits mine is just falling 1 image of 1 square. I would like to add more images and do not know how to create. For example, that they were 3 squares; a yellow, a green and a blue, I want to show them repeatedly on the screen like the fruit ninja. Randomly he calls an image and presents it on the screen. My code so far, I wanted to create a method that would do this but I do not know how !!

public class Inimigo extends Retangulo{ 
    private static Bitmap bmp; 
    public Inimigo(int x, int y, Resources res){
        super(x, y, 40, 40); 
        if(bmp==null){ 
            //instancio a imagem do resource 
            bmp = BitmapFactory.decodeResource(res, R.drawable.amarelo); 
            //redimensiona a imagem 
            bmp = Bitmap.createScaledBitmap(bmp, 40, 40, true); 
        }
    } 
    public void mexe(int height,int width){ 
        if (getY()<height){ 
            setY(getY()+5); 
        } 
        else{ 
            int x = (int)(Math.random()*(width-25));
            setX(x); setY(-25); 
        }
    } 
    public void draw(Canvas canvas, Paint paint){ 
        canvas.drawBitmap(bmp, getX(), getY(), paint);
    }
}
    
asked by anonymous 13.05.2015 / 03:29

1 answer

1

From what I understand are 3 difficulties:

  • Assigning a value to each image
  • Sorting one of 3 images
  • Display an image every 'x' seconds

Begin by declaring two arrays , one with the ids of the Drawables you will use and the other with the values assigned to them.

int[] varImagens = {
    R.drawable.blue,
    R.drawable.grenn,
    R.drawable.yeollow
};

int[] varValores = {
    1,
    2,
    3
};

Note: If the values to be assigned are these or sequential the array will not be necessary. The value can be calculated according to the position (index) that the image occupies.

To sort an image use:

int posicao = new Random().nextInt(varImagens.length);
int imagemId = varImagens[posicao];
int valor = varValores[posicao];
// ou
// int valor = posicao + 1;

In order for the images to alternate every 'x' seconds, use a Timer :

private final Handler handler = new Handler();
private final boolean isRunnig = false;
-------------------

Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {

        handle.post(new Runnable() {
            @Override
            public void run() {
                if(isRunnig){
                    int posicao = new Random().nextInt(varImagens.length);
                    mostraImagem(posicao);
                }
            }
        });
    }
}, 0, 3000); //Uma nova imagem é selecionada a cada 3 segundos

Within the run() method, the isRunnig variable is used to control execution. You will have to write the mostraImagem(int posicao) method which, according to the value passed, will show the corresponding image. The method will be called every 3 seconds if isRunnig is true

This is just a starting point.
It will have to be you to do the implementation.

Note: All code was written directly in the response box. I apologize for any syntax error.

    
14.05.2015 / 16:10