This site requires JavaScript, please enable it in your browser!
Greenfoot back
mandyg233
mandyg233 wrote ...

2014/5/21

Creating and deleting objects (or hiding them and making them visible again)

mandyg233 mandyg233

2014/5/21

#
Hi, so i have a boat class, and in a different class (Victor class) I'm trying to delete the boat, and also I need to bring back the boat at a later time. i know I have to make it into an object, but I'm not sure how to do that.
mandyg233 mandyg233

2014/5/21

#
Also, the boat is an object, and won't have anything like a string involved
danpost danpost

2014/5/21

#
If it can be another boat created from the same class, then in the Victor class, use 'getWorld().removeObject(boat);', where 'boat' references the Boat object to remove, and use 'getWorld().addObject(new Boat(), getX()+??, getY()+??);' to recreate one. If it has to be the same exact boat, the add an 'Actor boat' field to the Victor class to store the reference to that Boat object and use 'getWorld().addObject(boat, getX()+??, getY()+??);' to re-insert the same boat into the world.
mandyg233 mandyg233

2014/5/21

#
Will that remove the image though if its a different boat object?
mandyg233 mandyg233

2014/5/21

#
to reference boat would it just be Boat boat = new Boat(); ?
mandyg233 mandyg233

2014/5/21

#
Because right now that does not seem to be working
mandyg233 mandyg233

2014/5/21

#
The boat is not being removed
mandyg233 mandyg233

2014/5/21

#
it says cannot find symbol - variable boat if (getX() >1800) { // getWorld().removeObject(this); // getWorld().removeObject(Boat.class); Greenfoot.delay(50); getWorld().removeObject(boat); getWorld().setBackground(new GreenfootImage("Blinklala1.png")); Greenfoot.delay(20); getWorld().setBackground(new GreenfootImage("Blinklala2.png")); Greenfoot.delay(20); getWorld().setBackground(new GreenfootImage("Blinklala1.png")); Greenfoot.delay(30); letsGo(); } }
danpost danpost

2014/5/21

#
mandyg233 wrote...
Will that remove the image though if its a different boat object?
No. You need to remove the boat that is already in the world.
mandyg233 wrote...
to reference boat would it just be Boat boat = new Boat(); ?
This will reference a new boat, not the one in the world. Where is the boat when your actor gets to x > 1800 ? Is there only one boat in the world ?
mandyg233 mandyg233

2014/5/21

#
yes there is only one boat in the world. how do i remove that boat?
danpost danpost

2014/5/21

#
1
getWorld().removeObjects(getWorld().getObjects(Boat.class));
will remove the boat; and
1
getWorld().addObject(new Boat(), /* new location coordinates */);
will add a new one back into the world. If you want to have the same boat added back into the world later, then
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// add instance field to Victor class
private Boat boatRemoved = null;
// to save boat being removed
if (getX() > 1800 && boatRemoved == null)
{
    boatRemoved = (Boat)getWorld().getObjects(Boat.class).get(0);
    getWorld().removeObject(boatRemoved);
}
// and add back into the world later with
if (boatRemoved != null)
{
    getWorld().addObject(boatRemoved, /* new location coordinates */);
    boatRemoved = null;
}
You need to login to post a reply.