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

Asked

Viewed 167 times

1

I’m developing a game and it’s just randomly generating an image on the screen. I would like to implement 3 different images, which through a random were appearing automatically on the screen, and that each image had a value.

Example:

imagem1 = 1;
imagem2 = 2;

Imagine the game Fruit ninja!! Mine is similar, only in place of the fruits mine is just falling 1 picture of 1 square.
I would like to add more images and do not know how to create.
For example, that were 3 squares; a yellow, a green and a blue, I want to show them repeatedly on the screen equal to Fruit ninja.
Randomly it draws an image and displays on the screen.
My code so far, I wanted to create a method that would do this but I don’t 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);
    }
}

1 answer

1


From what I understand are 3 your difficulties:

  • Assign a value to each image
  • Draw one of 3 images
  • Display a picture every x seconds

Start by declaring two arrays, one with the ids of Drawables which 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 it won’t be necessary.
The value can be calculated according to the position (index) that the image occupies.

To draw an image use:

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

So that the images change every x seconds use one 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 method run() the variable is used isRunnig to control the execution.
You have to write the method mostraImagem(int posicao) which, according to the value passed, will show the corresponding image.
The method will be called every 3 seconds if isRunnig for true

This is just a starting point.
You will have to do the implementation.

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

  • Huuuum understood how to do, thank you very much!! THANKS MESMOOOO, I don’t know how to thank you!! success for you always, yes the running method is already ready was just that even I didn’t know how to do it.. Thanks!! : D :D

  • In stackoverflow the best way to thank who helped you is to mark the best answer and/or vote for those who were useful.

  • The code above didn’t work! not from the error but it didn’t work, as I do to draw (position) in the draw method that takes bmp as the image that is inside the Imimigo public Enemy(int x, int y, Resources res) { super(x, y, 40, 40); if (bmp==null) { //instancio a Resource //bmp = Bitmapfactory.decodeResource(res, R.drawable.yellow); ///resizes image bmp = Bitmap.createScaledBitmap(bmp, 40, 40, true); } private Bitmap bmp; public draw(Canvas, Paint Paint) { canvas.drawBitmap(bmp, getx(), gety(), Paint); }

  • I don’t know if I understand your question. But to move the image, based on the question code, you must call the method mexe with the new coordinates and call the method postInvalidate() of view, which will cause the rectangle to be drawn in the new position.

  • that right there, the logic is that I’m just not getting draw on the screen at the same time... ie run, when I run the application closes..

  • I think it would be better to create a new question, so more people can help. Put the relevant part of the code and the error log.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.