I have an arraylist that uses mouse clicks to select random elements, which are then removed from the list. When the list is empty, I want to refresh with a click and start over. The way I am trying this is by creating a new list, adding removed elements to it, then setting the old list equal to it.
myTiles = newTiles;//refresh myTiles with all elements
newTiles.clear();//clear newTiles to start over
It’s not quite working. Help appreciated. Here’s all the code:
int index;
//array of Tile objects
ArrayList <Tile> myTiles = new ArrayList <Tile>();
//also create a second array list to 'reset' from
ArrayList <Tile> newTiles = new ArrayList <Tile>();
void setup(){
background(0);
size(300,300);
myTiles.add(new Tile("apple", loadImage("fruit0.jpg")));
myTiles.add(new Tile("orange", loadImage("fruit1.jpg")));
myTiles.add(new Tile("pear", loadImage("fruit2.jpg")));
textAlign(CENTER);
textSize(35);
text("Random Fruits",width/2,height/3);
text("Click to play",width/2,2*height/3);
}
void draw(){
}
void mouseClicked(){
if(myTiles.size() > 0){
index = int(random(myTiles.size()));
myTiles.get(index).display();
}else{
background(0);
textSize(25);
text("Out of Names", width/2,height/3);
text("Click to Play Again", width/2,2*height/3);
myTiles = newTiles;//refresh myTiles with elements
newTiles.clear();//clear newTiles to start over
}
}
}
//Tile object
class Tile{ //Tile knows an image and name
PImage img; //pulls in image
String item; //pulls in name
Tile(String tempItem, PImage tempImg){//constructor
item = tempItem; //name pulled in
img = tempImg; //image pulled in
}
void display(){
background(0);
textSize(70);
fill(255,0,0);
text(item, width/2, 3*height/4);
imageMode(CENTER);
img.resize(100,100);
image(img,width/2,height/4);
myTiles.remove(index);
newTiles.add(new Tile(item,img));
}
}