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

2012/11/6

Removing Objects

jdc4aub jdc4aub

2012/11/6

#
I am creating a game where the user hits the enemy and the enemy dies (is removed from game). If the user is is not pressing the hit command and an enemy gets too close, the enemy dies and the user loses a health point. I'm using the getObjectsInRange and the getWorld.removeObjects commands to do this, but the enemies pass right by the user no matter if the user is hitting or not. Upon inspecting the user, the health points have not decreased. I tested the range of the getObjectsInRange command by setting a range that was unreasonably large compared to the game world itself and still there is no loss of health points. Any ideas to what my problem may be?
danpost danpost

2012/11/6

#
Some code would be helpful in finding your problem. It could be that you have created more than one 'int' for the health of the actor and not getting the value from the one that is changing.
jdc4aub jdc4aub

2012/11/6

#
public class User extends Actor { int h; private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; private GreenfootImage image4; /** * Act - do whatever the Zombie wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public User() { image1 = new GreenfootImage("User Badass.png"); image2 = new GreenfootImage("User Badass Punch.png"); image3 = new GreenfootImage("User Badass Flip.png"); image4 = new GreenfootImage("User Badass Punch Flip.png"); } public void act() { if (Greenfoot.isKeyDown("left")) { setImage("User Badass Flip.png"); if (Greenfoot.isKeyDown("space")) { setImage("User Badass Punch Flip.png"); } } else if (Greenfoot.isKeyDown("right")) { setImage("User Badass.png"); if (Greenfoot.isKeyDown("space")) { setImage("User Badass Punch.png"); } } if (Greenfoot.isKeyDown("space")) { if (getObjectsInRange (500,Zombie.class).size() > 0) { getWorld().removeObject(this); } } else if (getObjectsInRange (100,Zombie.class).size() > 0) { h = h++; getWorld().removeObject(this); } if (h == 3) { Greenfoot.stop(); } } }
danpost danpost

2012/11/6

#
I am not sure if this would be causing your problem, but I have never seen an expression as this
h = h++;
Maybe you meant 'h++;' or 'h += 1;' or 'h = h + 1;' (which all mean the same thing). Your getWorld().removeObject(this) statements will remove the User actor (not what you want here).
jdc4aub jdc4aub

2012/11/6

#
I tried the "h++" and "h=h+1" before going to "h = h++" just to try and see if that could work. How would I reference the enemy to remove it instead of the user?
danpost danpost

2012/11/6

#
if (!getObjectsInRange(500, Zombie.class).isEmpty())
{
    Zombie zmb = (Zombie) getObjectsInRange(500, Zombie.class).get(0);
    getWorld().removeObject(zmb);
}
Of course, this will remove one Zombie object from the world. Which one would be a guessing game; as the range would pretty much cover the whole world.
You need to login to post a reply.