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);
}
}
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
– Mateus Santin Junior
In stackoverflow the best way to thank who helped you is to mark the best answer and/or vote for those who were useful.
– ramaral
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); }
– Mateus Santin Junior
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 methodpostInvalidate()
of view, which will cause the rectangle to be drawn in the new position.– ramaral
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..
– Mateus Santin Junior
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.
– ramaral