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

2014/4/24

How do you add an object back to the world after it's removed?

Tommy99 Tommy99

2014/4/24

#
Hello. I am trying to have my "Bee" class remove my "SuitMan" class while at the same time subtracting 1 life and adding another SuitMan to the World. It does subtract the life and removes it, but it doesn't add another one to the world. Here is the code in the Bee class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Bee extends Actor { private int hSpeed = 3; GreenfootImage img = new GreenfootImage("bee-left.png"); GreenfootImage img2 = new GreenfootImage("bee-right.png"); public void act() { setLocation(getX()+hSpeed, getY()); if(getX()<50){ setImage(img2); hSpeed = -hSpeed; } if(getX()>700){ setImage(img); hSpeed = -hSpeed; } SuitMan sm = (SuitMan)getOneIntersectingObject(SuitMan.class); if(sm!=null){ sm.lessLives(); getWorld().addObject(sm, 11, 442); getWorld().removeObject(sm); } } public void explode() { Greenfoot.playSound("Explosion.wav"); } } Here is the code in the SuitMan class: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class SuitMan extends Actor { private final double GRAVITY = 1.2; private final int JUMP_STRENGTH = 15; private GreenfootImage left = new GreenfootImage("man-left.png"); private GreenfootImage right = new GreenfootImage("man-right.png"); private double vSpeed = 0.0; private int speed = 5; private int level = 1; private boolean firing=false; private Scoreboard myScore; private Scoreboard myBullets; private Scoreboard myLives; private boolean moreBullets = true; public SuitMan(Scoreboard score, Scoreboard bullets, Scoreboard lives) { myScore = score; myBullets = bullets; myLives = lives; } public Scoreboard getScoreboard() { return myScore; } public Scoreboard getScoreboard2() { return myBullets; } public Scoreboard getScoreboard3() { return myLives; } public void act() { checkKeys(); Actor ol = getOneIntersectingObject(OpenLift.class); if(ol!=null) { if(level==1) { Greenfoot.setWorld(new SpaceWorld2(this)); } if(level==2) { Greenfoot.setWorld(new SpaceWorld3(this)); } level++; } Actor a = getOneIntersectingObject(Airplane.class); if(a!=null) { Greenfoot.stop(); } if(!onAnything()) { fall(); } } public void jump() { if(onAnything()) { vSpeed = -JUMP_STRENGTH; fall(); } } public void fall() { vSpeed = vSpeed + GRAVITY; setLocation( getX(), (int)(getY()+vSpeed) ); if(getY()>getWorld().getHeight()-3){ lessLives(); getWorld().addObject(new SuitMan(myScore, myBullets, myLives), 11, 442); getWorld().removeObject(this); } } public void checkKeys() { if(Greenfoot.isKeyDown("left")) { setLocation(getX()-speed,getY()); setImage(left); } if(Greenfoot.isKeyDown("right")) { setLocation(getX()+speed,getY()); setImage(right); } if(Greenfoot.isKeyDown("up")) { jump(); } if(Greenfoot.isKeyDown("space")){ if( firing==false){ //casting if(haveBullets()){ lessBullets(); getWorld().addObject(new Bullet(), getX(), getY()); Greenfoot.playSound("EnergyGun.wav"); } firing=true; } } if(!Greenfoot.isKeyDown("space")){ firing=false; } } public void lessBullets() { myBullets.changeScore(-1); if(myBullets.getScore()==0){ moreBullets = false; } } public boolean haveBullets() { return moreBullets; } public boolean onAnything() { int dy = getImage().getHeight()/2; Actor cliff = getOneObjectAtOffset(0,dy,null); return cliff != null; } public void lessLives() { myLives.changeScore(-1); if(myLives.getScore()==0){ Greenfoot.setWorld(new GameOver2()); } } } Thank you, I really appreciate it.
danpost danpost

2014/4/24

#
It would make more sense to remove the SuitMan object from the world before adding it back in.
getWorld().removeObject(sm);
getWorld().addObject(sm, 11, 442);
However, why not just relocate the actor:
sm.setLocation(11, 442);
BTW, you are only dealing with one SuitMan object either way. With your original code and with what you said -- 'it doesn't add another one to the world' -- 'another' should be 'the same'. You never used 'new SuitMan()' to create another one (and you do not need to).
Tommy99 Tommy99

2014/4/24

#
I went with the 2nd way. Thanks, I didn't even think of it, and it's such a simple solution to this problem.
Tommy99 Tommy99

2014/4/24

#
Also, do you know how to shoot a bullet to the left side if my character is facing left? Currently when my character is facing left, it still shoots to the right side. Here is the code for the Bullet Class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.*; public class Bullet extends Actor { private static final int SPEED=10; private GreenfootImage left = new GreenfootImage("man-left.png"); private GreenfootImage right = new GreenfootImage("man-right.png"); public void act() { move(SPEED); if(getX()>getWorld().getWidth()-5) { getWorld().removeObject(this); } else { Alligator gator = (Alligator)getOneIntersectingObject(Alligator.class); if(gator !=null) { gator.explode(); getWorld().addObject(new Explosion(),getX(),getY()); getWorld().removeObject(gator); getWorld().removeObject(this); } else { Snake snake = (Snake)getOneIntersectingObject(Snake.class); if(snake !=null) { snake.explode(); getWorld().addObject(new Explosion(),getX(),getY()); getWorld().removeObject(snake); getWorld().removeObject(this); } else { Bee bee = (Bee)getOneIntersectingObject(Bee.class); if(bee!=null){ bee.explode(); getWorld().addObject(new Explosion(),getX(),getY()); getWorld().removeObject(bee); getWorld().removeObject(this); } else { Hedgehog hedgehog = (Hedgehog)getOneIntersectingObject(Hedgehog.class); if(hedgehog!=null){ hedgehog.explode(); getWorld().addObject(new Explosion(),getX(),getY()); getWorld().removeObject(hedgehog); getWorld().removeObject(this); } } } } } } public Bullet() { GreenfootImage image = new GreenfootImage(10,3); image.setColor(Color.RED); image.fill(); setImage(image); } }
danpost danpost

2014/4/24

#
When your character creates the bullet, if it is facing left, turn the bullet around:
Bullet bullet = new Bullet();
if ( /* facing left */ ) bullet.setRotation(180);
getWorld().addObject(bullet, getX(), getY());
danpost danpost

2014/4/24

#
The condition should probably be:
if (getImage() == left)
You need to login to post a reply.