I have to do my homework in Greenfoot
.
This part means that I have to save the position of Chess
and then click reset.
Then, I have to choose load to put back the pieces of chess in the position they had before reset.
Since I don't know the exact size of the array, I know I can use List
but it isn't allowed in the homework.
Nothing showed up on the screen but there is no error message.
Assume I have a class called Chess
.
static Actor[] allChess;
public void save() // this is the save
{
Actor[] allChess = GWorld.getAllObjects("Chess");
}
public void load() // this is the load
{
if (allChess != null)
{
for (int i=0; i < allChess.length; i++)
{
Chess ch = (Chess) allChess[i];
GWorld.addOneObject(new Chess(ch.color, ch.rank), ch.getX(), ch.getY());
}
}
}
Thanks a lot!
allChess
is redeclared as a local variable in save()
. Do like this :
public void save() // this is the save
{
allChess = GWorld.getAllObjects("Chess");
}