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

2013/12/22

remove health

1
2
NaomiMenzo NaomiMenzo

2013/12/22

#
Hello, If i made an actor were i made a lifebar and called it in the world class, and i want to remove 1 health if 2 of my other actors hit eachother. wheres the best place to write this code? in the world or actor class? This is my health class (thanks to DanPost) if its necessary to know. thanks in advance!
public class heart extends Actor

{

    
  public static final boolean HORIZONTAL = false;  
  private static final int GAP = 10;  
  private boolean orientation = HORIZONTAL;  
  private GreenfootImage icon;  
  private int count;
  private boolean direction;
  private int initValue;  
  private int value;
  

  
  public vie()  
  {  
    icon = new GreenfootImage("viegr.png");  
    count = 3;
    direction = false;
    value = initValue = count;  
    orientation = direction;  
    updateImage();  
  }  
  
  private void updateImage()  
  {  
    int across = value;  
    int down = 1;  
    int xGap = GAP;  
    int yGap = 0;  
    if (orientation == VERTICAL)  
    {  
      across = 1;  
      down = value;  
      xGap = 0;  
      yGap = GAP;  
    }  
    int wide = icon.getWidth()*across+xGap*(across+1);  
    int high = icon.getHeight()*down+yGap*(down+1);  
    GreenfootImage base = new GreenfootImage(wide, high);  
    for (int j=0; j<down; j++) for (int i=0; i<across; i++)  
    {  
      int xOffset =xGap+i*(xGap+icon.getWidth());  
      int yOffset = yGap+j*(yGap+icon.getHeight());  
      base.drawImage(icon, xOffset, yOffset);  
    }  
    setImage(base);  
  }  
  
  public void adjustValue(int change)  
  {  
    if (value+change > initValue || value + change < 0) return;  
    value += change;  
    updateImage();  
  }  
  
  public int getValue()  
  {  
    return value;  
  }  
danpost danpost

2013/12/22

#
Well, the collision checking between the two actors that hit each other would be in one of the classes of the actors that collide. From there you can access the world with 'getWorld' and type-cast it to your world subclass name:
WorldSubclassName subWorld = (WorldSubclassName) getWorld();
From there, it would depend on whether you have a reference to the lifebar or not; where the reference is, if you do have one; if in the class of the actor whose health is in question; if no reference exists, is it the only lifebar in your world; etc. Anyway, it would come down to something like this:
subWorld.lifebarReferenceName.adjustValue(-1);
if (subWorld.lifebarReferenceName.getValue() == 0) gameOver();
If you only have one lifebar object in the world and no reference to it, then:
heart lifebarReferenceName = (heart) subWorld.getObjects(heart.class).get(0);
NaomiMenzo NaomiMenzo

2013/12/22

#
From there, it would depend on whether you have a reference to the lifebar or not; where the reference is, if you do have one; if in the class of the actor whose health is in question; if no reference exists, is it the only lifebar in your world; etc. Anyway, it would come down to something like this: view plaincopy to clipboardprint? subWorld.lifebarReferenceName.adjustValue(-1); if (subWorld.lifebarReferenceName.getValue() == 0) gameOver(); If you only have one lifebar object in the world and no reference to it, then: view plaincopy to clipboardprint? heart lifebarReferenceName = (heart) subWorld.getObjects(heart.class).get(0);
Thanks for answering Danpost, Got a quick questing about the "lifebarReferenceName". I got these 3 actors, Plane, Enemy and Heart. iam trying to place the substract health method in the Plane actor.
heart lifebarReferenceName = (heart) subWorld.getObjects(heart.class).get(0); 
I should place this part in the actor where the collision between the plane actor and the enemy actor take place right?
danpost danpost

2013/12/22

#
Yes. Followed by 'lifebarReferenceName.adjustValue(-1);', etc. To avoid confusion, I should inform you that the 2-liner above using 'subWorld.lifebarReferenceName' would be used only if you already had a reference to the lifebar in the world class.
NaomiMenzo NaomiMenzo

2013/12/22

#
private void heart()
    {
        heart heart = new heart();
        addObject(heart, 142, 58);
        
    }
If i put it like this in the World. I should replace the 'lifebarReferenceName.adjustValue(-1);' with 'heart' right?
danpost danpost

2013/12/22

#
NaomiMenzo wrote...
private void heart()
    {
        heart heart = new heart();
        addObject(heart, 142, 58);
        
    }
If i put it like this in the World. I should replace the 'lifebarReferenceName.adjustValue(-1);' with 'heart' right?
Not right. With 'heart' being declared in the method, it becomes local to the method; meaning that when the method is done executing, the reference is lost. Moving line 3 to outside the method will make it an instance field (a reference that belongs to an instance of that class -- the active world, in your case). Sidenote: method names should indicate what they do. 'heart' as a method name does not tell you want it does. 'addHeart' would be more informative and make the code that calls the method more readable.
NaomiMenzo NaomiMenzo

2013/12/22

#
Thanks! Changed the heart to addHeart and more cause even i get lost sometimes. But iam still stuck at the 'lifebarReferenceName.adjustValue(-1);' even though i add
Sky subWorld = (sky) getWorld(); 
    heart lifebarReferenceName = (heart) subWorld.getObjects(heart.class).get(0); 
     
    subWorld.lifebarReferenceName.adjustValue(-1);  
    if (subWorld.lifebarReferenceName.getValue() == 0) gameOver(); 
in the act method. It keeps saying that it cant find variable lifebarReferenceName. Shouldnt
 heart lifebarReferenceName = (heart) subWorld.getObjects(heart.class).get(0); 
be the variable its looking for?
danpost danpost

2013/12/22

#
Line 4 does not need the 'subWorld.' referencing as it was used to create the heart reference.
Sky subWorld = (sky) getWorld(); 
heart heartObject = (heart) subWorld.getObjects(heart.class).get(0); 
heartObject.adjustValue(-1);  
if (heartObject.getValue() == 0) gameOver();
I changed the reference name to something more appropriate; though, class names should begin with uppercase letters (by convention) which would make coding this easier with 'Heart heart = (Heart) ...'. Also, unless you wrote a 'gameOver' method, you should replace 'gameOver();' with curly brackets, '{ }' (or just ';') until you do create one.
NaomiMenzo NaomiMenzo

2013/12/22

#
Thanks :D ! i got closer to what i wanted, but i still get this message. Could you tell me why this appears? java.lang.NullPointerException at barrel.removeHealth(Barrel.java:50) at barrel.act(Enemy.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
public void removeHealth()
    {
     Sky subWorld = (Sky) getWorld();   
     Heart heartObject = (Heart) subWorld.getObjects(Heart.class).get(0);   
     heartObject.adjustValue(-1);    
     if (heartObject.getValue() == 0);  
    }
public void act()
   {
       
    if(Hit())  
    {  
       getWorld().removeObject(this);   
       removeHealth();
    }  
    else  
    {  
      int x = Greenfoot.getRandomNumber(2);  
      move(-x);  
    }       
    
 
       
    if(atWorldEdge())  
    {  
       getWorld().removeObject(this);   
    }  
    else  
    {  
      int x = Greenfoot.getRandomNumber(2);  
      move(-x);  
    }  
Thanks for taking the time helping me Danpost!
danpost danpost

2013/12/22

#
Swap lines 6 and 7 of your act method. You cannot get the world from the actor after removing it from the world.
NaomiMenzo NaomiMenzo

2013/12/22

#
Now that i think about it. It doesnt make sense what i did there. But now that they are swaped, it works fine, but i got in my Heart.class 3 hearts. But the game stops after i made contact with the first enemy. It removes 1 heart though, but the game stops with 2 hearts left. You know where i forgot something?
danpost danpost

2013/12/22

#
Do it stop without throwing any exceptions? or is there an error message? If an error message exists, show the error and the code around which it occurred. If no error message, find in your code where you use 'Greenfoot.stop();'. Look at code around this.
NaomiMenzo NaomiMenzo

2013/12/22

#
Yea there still is an error message, but i thought that it didnt matter really cause when i compiled it didnt find any syntax errors. java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Enemy.atWorldEdge(barrel.java:68) at Enemy.act(barrel.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
public boolean atWorldEdge()  
    {  
     return getX() == 0 ||   
               getX() == getWorld().getWidth() - 1 ||  
               getY() == 0 ||
               getY() == getWorld().getHeight() - 1;  
    } 
public void act()
   {
       
    if(Hit())  
    {  
       removeHealth();
       getWorld().removeObject(this);   
       
    }  
    else  
    {  
      int x = Greenfoot.getRandomNumber(2);  
      move(-x);  
    }       
       
    if(atWorldEdge())  
    {  
      getWorld().removeObject(this);   
    }  
    else  
    {  
      int x = Greenfoot.getRandomNumber(2);  
      move(-x);  
    }     
    
  
    }
danpost danpost

2013/12/22

#
If the first 'if' block in your act method removes the actor from the world, then the second one will error because you cannot run 'getX' on an actor that is not in a world. Change line 16 of the act method to this:
if (getWorld() != null && atWorldEdge())
No. The 'else' part will still err on 'move'. Insert before the 'if(atWorldEdge())' statement the following:
if (getWorld() == null) return;
or, maybe better is to insert in the first 'if' block after the 'removeObject' statement a 'return;' statement.
NaomiMenzo NaomiMenzo

2013/12/22

#
Worked like a charm :D ! But i still get the same error message, why do i still get it? it does what its supposed to do. java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Enemy.atWorldEdge(Enemy.java:68) at Enemy.act(Enemy.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
There are more replies on the next page.
1
2