1

I have 5 objects that created Randomly after a certain time in a SurfaceView,
I draw them on the Draw method like this :

bby.draw(canvas);
hyd.draw(canvas);
hole.draw(canvas);

my problem is that the objects won't draw randomly!
for E.g : if hyd was created before bby it won't draw until bby get creaed and drawn and the same with others.
I think is because the Draw method draw objects in order of the lines like :

1 bby.draw(canvas);
2 hyd.draw(canvas);
3 hole.draw(canvas);

but i don't want that.
How can i fix this? (Hopefully you got my point)

Ethan
  • 25
  • 4

2 Answers2

0

I think is because the Draw method draw objects in order of the lines like :

it is because every object you define is going to be drawn in the canvas only when you call the draw method... but this is not going to happen asynch, i.e. in this sequence :

bby.draw(canvas);
hyd.draw(canvas);
hole.draw(canvas);

is what the objects are going to be drawn

you can generate a random number between a and b and change the order of draw according to that number...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

If you put your drawable objects in a list, you can access a random index to create a random behavior. List's remove method makes this easy to only draw each once.

Random rand = new Random();
ArrayList<Draw> list = new ArrayList<Draw>();
// Add your elements here
while (!list.isEmpty()) {
    // Gets a random index in the range of the list, then removes and returns it.
    Draw elem = list.remove(rand.nextInt(list.size()));
    // Do your drawing with your random Draw elem here
}
MattMerr47
  • 69
  • 5