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

2014/4/21

Removing an actor at the same time another actor is removed

patrick26 patrick26

2014/4/21

#
Hi, I initially added a new Enemyfireball in my world class in a position so it would look like my actor Plant was shooting fireballs. However, I am having trouble removing the Enemyfireball from the world once the plant is removed from the world. Does anyone know how to fix this problem? Thanks for the help.
danpost danpost

2014/4/21

#
Remove the fireballs before removing the plant.
patrick26 patrick26

2014/4/22

#
I am still unable to do this. In my fireball class, I have it so that whenever it hits the plant, the plant is removed. However, the Enemyfireball continues to shoot. I am not sure how to remove the Enemyfireballs. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Fireball here. * * @author (your name) * @version (a version number or a date) */ public class Fireball extends Actor { private static final int SPEED=7; /** * Act - do whatever the Fireball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(SPEED); if(getX()>getWorld().getWidth()-3) { getWorld().removeObject(this); return; } Plant plant = (Plant)getOneIntersectingObject(Plant.class); if(plant!=null) { plant.explosion(); getWorld().addObject(new Hugeexplosion(),getX(),getY()); getWorld().removeObject(plant); getWorld().addObject(new Jumper(),195,460); }
danpost danpost

2014/4/22

#
Where is the code for creating the Enemyfireball objects? That is the code that we probably need to work with. Also, the 'explosion' method in the plant class might be important.
patrick26 patrick26

2014/4/22

#
I actually added the Enemyfireball in the subclass of world. I changed a bit of the code and put the code I had above into the plant class shown below. The 'explosion' method is shown but it merely just adds debris to the explosion to make it seem more realistic. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Plant here. * * @author (your name) * @version (a version number or a date) */ public class Plant extends Actor { /** * Act - do whatever the Plant wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Fireball fireball = (Fireball)getOneIntersectingObject(Fireball.class); if(fireball!=null) { explosion(); getWorld().addObject(new Hugeexplosion(),getX(),getY()); getWorld().removeObject(this); getWorld().addObject(new Jumper(),195,460); } Fireball2 fireball2 = (Fireball2)getOneIntersectingObject(Fireball2.class); if(fireball2!=null) { explosion(); getWorld().addObject(new Hugeexplosion(),getX(),getY()); getWorld().addObject(new Jumper(),195,460); getWorld().removeObject(this); } } public void explosion() { for(int h=0; h<60; h++) { getWorld().addObject(new Plantdebris(),getX(),getY()); } } }
patrick26 patrick26

2014/4/22

#
My Enemyfireball class is shown below. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Enemyfireball here. * * @author (your name) * @version (a version number or a date) */ public class Enemyfireball extends Actor { private int vSpeed; private int hSpeed; /** * Act - do whatever the Enemyfireball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { plant(); moveAround(); vSpeed = Greenfoot.getRandomNumber(7); hSpeed = 7; } public void moveAround() { setLocation(getX()+hSpeed,getY()+vSpeed); } public void plant() { if(getX()>getWorld().getWidth()-3) { setLocation(Greenfoot.getRandomNumber(60)+170,Greenfoot.getRandomNumber(80)+215); } Actor ground = getOneIntersectingObject(Ground.class); if(ground!=null) { setLocation(Greenfoot.getRandomNumber(60)+170,Greenfoot.getRandomNumber(80)+215); } } }
danpost danpost

2014/4/22

#
I think you need to do this: only create Enemyfireball objects from your subclass of world while the plant is in the world. In other words:
if (!getObjects(Plant.class).isEmpty()) addObject(new Enemyfireball(), Greenfoot.getRandomNumber(60)+170, Greenfoot.getRandomNumber(80)+215);
patrick26 patrick26

2014/4/22

#
So do I add that code to my subclass of world? Do I need to delete any code from my Enemyfireball class?
danpost danpost

2014/4/22

#
It would go in your subclass of world, modifying what you already have to add Enemyfireball objects into the world. As far as deleting anything from the Enemyfireball class, I am not sure at the moment. Let us see how things go after you modify your adding of Enemyfireball objects into the world, first. You may not have to change anything in the Enemyfireball class.
patrick26 patrick26

2014/4/22

#
Unfortunately, after the plant is removed the Enemyfireball continues to fire.
danpost danpost

2014/4/22

#
Ok, try this. In your Enemyfireball class, add the following method
private boolean canRespawn()
{
    return !getWorld().getObjects(Plant.class).isEmpty();
}
Then, change both 'setLocation' lines in your 'plant' method to this:
if (canRespawn) setLocation... // continued
else
{
    getWorld().removeObject(this);
    return;
}
You need to login to post a reply.