In the quiz I'm making, I have a method in the world to generate boxes with the different answers on them. The problem I'm having is that with the method I'm using, sometimes the boxes end up on top of each other so one will be hidden under the other. Here's what that code looks like:
I tried to fix this by adding this method to the Button class, but the method made the buttons constantly move instead of only move when another button was already there.
Do you guys have any recommendations on how to fix my fixPlacement method? Or should I try something else entirely to prevent the buttons from overlapping each other?
private void createAnswerBank()
{
int x = 7*getWidth()/8;
int y = 14*getHeight()/125;
int z = 7*getHeight()/125;
for (int i = 0; i<16; i++)
{
Button button = new Button(getHeight()/10 +(z*i), score, numberCorrect);
a = Greenfoot.getRandomNumber(16);
b = y+(z*a);
addObject(button, x, b);
String image = new String(polyatomicFormulas[i]);
button.setImage(image);
}
}public void fixPlacement()
{
World world = getWorld();
int y = 14*world.getHeight()/125;
int z = 7*world.getHeight()/125;
if (isTouching(Button.class))
{
setLocation(7*world.getWidth()/8, y+(z*Greenfoot.getRandomNumber(16)));
}
}
