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

2016/3/2

Could someone tell me why this isnt working? :/

Sangate Sangate

2016/3/2

#
public int energyValue = 100;   


  public void lifeCheck (){
        if (energyValue == 0  )   
        {
                             ( JUST A PART OF THE CODE )
Actor actor = getOneIntersectingObject(Crab.class);
getWorld().removeObject(actor);

}
if (energyValue > 0 )   
        {
      
Actor actor = getOneIntersectingObject(Crab.class);
getWorld().removeObject(actor);

}
( energyValue falls under 0 / -1 , thats the problem )
Sangate Sangate

2016/3/2

#
in 12 it´s if (energyValue < 0 ) ( Sorry copied wrong one )
davmac davmac

2016/3/2

#
Sangate wrote...
( energyValue falls under 0 / -1 , thats the problem )
You haven't shown any code which changes the value of energyValue, so there's not much anyone can say. Also you should try to explain what the code is supposed to do and compare this with what it actually does. (Finally, please indent your code properly. It's as simple as pressing Ctrl+shift+I so there's no excuse for not doing so.)
Sangate Sangate

2016/3/2

#
Okay, a second try : (OilDrop.class : ) public class OilDrop extends Actor { private int imageNumber; static GreenfootImage oilImage; public void act() { crabDesease(); switchImage(); if(atWorldEdge()){ edgeReboundDirection(); } move(); } public void switchImage(){ oilImage = new GreenfootImage; for(int i = 0; i < 12; i++){ oilImage = new GreenfootImage("oildrop"+i+".png"); setImage(oilImage); } imageNumber = imageNumber + 1; if(imageNumber == 11){ imageNumber = 0; } } public void edgeReboundDirection(){ if((getX()<20) || (getX()>(getWorld().getHeight()-20))){ setRotation(540 - getRotation()); } if(getY()<20 || getY()>getWorld().getHeight()-20){ setRotation(360 - getRotation()); } } public void crabDesease(){ Actor actor = getOneObjectAtOffset(0, 0, Crab.class); if(actor != null){ <------------------------ Here everything works fine. When she crab runs into the oil she loses 2 "energyValue" ((Crab)actor).energyValue =((Crab)actor).energyValue - 2; } } public boolean atWorldEdge() { if(getX() < 10 || getX() > getWorld().getWidth() - 10) return true; if(getY() < 10 || getY() > getWorld().getHeight() - 10) return true; else return false; } public void move() { move(4); } } ---------------------------------------------------------------------- When the crab runs into the oildrops she loses 2 "energyVlue" . She starts with 100 . When the crab has 0 oder > 0 energyValue she should "die" ---------------------------------------------------------------------- public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; public int energyValue = 100; public Crab() { wormsEaten = 0 ; image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage (image1); } public void act() { checkKeyPress(); lookForWorm(); switchImages (); move(); lifeCheck(); } public void turnAtEdge() { if(atWorldEdge()) { turn(20); } } public void randomTurn() { if(Greenfoot.getRandomNumber(100) < 10) { turn(30); } } public void lookForWorm(){ if(canSee(Worm.class)) { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten == 10 ) { Greenfoot.playSound ("victorys.wav"); } if (wormsEaten == 10 ) { Greenfoot.stop(); Greenfoot.setWorld(new CrabWorld()); } } } public void checkKeyPress() { if (Greenfoot.isKeyDown("left")) { turn (-5); } if (Greenfoot.isKeyDown("right")) { turn (5); } if (Greenfoot.isKeyDown("space")) { move(10); } } public int getWormsEaten() { return wormsEaten; } public void switchImages(){ if(getImage() == image1){ setImage(image2); } else { setImage(image1); } } public void lifeCheck (){ if (energyValue == 0 ) { ( <------------------------------------- This part is the problem. Nothing happens when "energyValue" is 0 . The crab should just die ) Actor actor = getOneIntersectingObject(Crab.class); getWorld().removeObject(actor); } } }
Super_Hippo Super_Hippo

2016/3/2

#
Please use the 'code' tag like you did in your first post. Like this, all the array brackets are lost.
Sangate Sangate

2016/3/2

#
public class Crab extends Animal
{
    private GreenfootImage image1;
    private GreenfootImage image2;  
    private int wormsEaten;
    public int energyValue = 100;

    public Crab()
    {
        wormsEaten = 0 ;
        image1 = new GreenfootImage("crab.png");
        image2 = new GreenfootImage("crab2.png");
        setImage (image1);
    }

    public void act()
    {

        checkKeyPress(); 
        lookForWorm();
        switchImages ();
        move(); 
        lifeCheck();

    }

    public void turnAtEdge() {

        if(atWorldEdge()) {

            turn(20);
        }
    }

    public void randomTurn() {
        if(Greenfoot.getRandomNumber(100) < 10)
        { 
            turn(30);
        } 

    }

    public void lookForWorm(){
        if(canSee(Worm.class))
        {
            eat(Worm.class); 
            Greenfoot.playSound("slurp.wav");
            wormsEaten = wormsEaten + 1;
            if  (wormsEaten == 10 ) 
            {
                Greenfoot.playSound ("victorys.wav");
            }

            if (wormsEaten == 10 ) 
            {             
                Greenfoot.stop();      
                Greenfoot.setWorld(new CrabWorld());
            }
        }
    }

    public void checkKeyPress() {
        if (Greenfoot.isKeyDown("left"))
        {
            turn (-5);
        }
        if  (Greenfoot.isKeyDown("right"))
        {
            turn (5);
        }
        if (Greenfoot.isKeyDown("space"))
        { 
            move(10);
        }

    }

    public int getLeavesEaten()
    {
        return wormsEaten;
    }

   

    public void switchImages(){
        if(getImage() == image1){
            setImage(image2);
        } else {
            setImage(image1);
        }
    }

     
    public void lifeCheck (){
        if (energyValue == 0  )   
        {

            Actor actor = getOneIntersectingObject(Crab.class);
            getWorld().removeObject(actor);
            Greenfoot.playSound ("oil.mp3");

        }
    }
}
------------------------------------------------------
OilDrops: 
public class OilDrop extends Actor
{
    private int imageNumber;
    static GreenfootImage oilImage[];
    
    public void act() 
    {
         crabDesease();
         switchImage();
        if(atWorldEdge()){
            edgeReboundDirection();
        }
        move();
    }

    public void switchImage(){
       
        oilImage = new GreenfootImage[12];
        for(int i = 0; i < 12; i++){
            oilImage[i] = new GreenfootImage("oildrop"+i+".png");

            setImage(oilImage[imageNumber]);
        
    }

        imageNumber = imageNumber + 1;
        if(imageNumber == 11){
            imageNumber = 0;
        }
    }

    public void edgeReboundDirection(){
        if((getX()<20) || (getX()>(getWorld().getHeight()-20))){
            setRotation(540 - getRotation());
        }
        if(getY()<20 || getY()>getWorld().getHeight()-20){
            setRotation(360 - getRotation());
        }
    }
   public void crabDesease(){
Actor actor = getOneObjectAtOffset(0, 0, Crab.class);
if(actor != null){
((Crab)actor).energyValue =((Crab)actor).energyValue - 2;


}
}
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
    public void move()
{
    move(4);
    
}
   
}

Super_Hippo Super_Hippo

2016/3/2

#
You can remove lines 98 and 99 and use:
getWorld().removeObject(this);
You want to remove the crab which doesn't have 'energy' anymore, not a crab that touches the crab without energy.
Sangate Sangate

2016/3/2

#
Wow thanks man!
You need to login to post a reply.