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

2015/7/20

when i was coding (trying to make a life counter i came up with problems im posting the code on making a life counter

1
2
swacks swacks

2015/7/20

#
code]code for life counter: import greenfoot.*; import java.awt.Color; /** * Write a description of class LifeCounter here. *
main character:

import greenfoot.*;

/**
 * a bob in the game will eat the Bobfood on your command.
 * 
 * @author (A.T.P) 
 * @version (2.4.2)
 */
public class Bob extends Beings
{

    private int points = 0;
    private Score Score;
    // instance field to add
    LifeCounter lifeCounter = new LifeCounter();
    // methods to add
    public LifeCounter getLifeCounter()
    {
        return lifeCounter;
    }

    public Bob(Score pointScore)
    {
        Score = pointScore;
    }

    /**
     * Act - do whatever the Bob wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(3);
        checkKeys();
        tryToEat();
    }    

    /**
     * once reached world edge you must turn away
     */
    public void turnAtWorldEdge()
    {  if (atWorldEdge())
        {
            turn(4);
        }
    }

    /**
     * check whether the keys are being pressed or not if they are follow the command
     */
    public void checkKeys()
    {
        if( Greenfoot.isKeyDown("left"))
        {
            turn(-6);   
        }
        if( Greenfoot.isKeyDown("right"))
        {
            turn(6);
        }
        if( Greenfoot.isKeyDown("up"))
        {
            move(5);   
        }
        if( Greenfoot.isKeyDown("down"))
        {
            move(-5);
        }
    }

    /**
     * Check whether we can see Bobfood.if we can,eat it.
     */
    void tryToEat()
    {
        if(canSee(BobFood.class))
        {
            eat(BobFood.class);
            Greenfoot.playSound("slurp.wav");
            Score.add(5);
            createNewBobFood();
        }

    }

    /**
     * add new BobFood into the world at a random location
     */
    private void createNewBobFood()
    {
        BobFood newBobFood;
        newBobFood=new BobFood();

        World world;
        world=getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);

        world.addObject(newBobFood,x,y);
    }

    private int getLivesCounter()
    {
        LifeCounter lives = ((BobWorld)getWorld()).getLivesCounter();  
    lives.add(-1);  
    if (lives.getValue() == 0) gameOver();  
}
    }

Need help with life counter wrote...
iv given you a list of code plz guide me to where the code is and shouldnt be and where it must be. i also need help with what i am missing in this code and if u could post what i need or what more info i should be giving ;)
* @author (your name) * @version (a version number or a date) */ public class LifeCounter extends Actor { private static final Color transparent = new Color(0,0,0,0); private GreenfootImage background; private int value; private int target; /** * */ public LifeCounter() { background = getImage(); value = 3; target = 3; updateImage(); } /** * Animate the display to count up (or down) to the current target value. */ public void act() { if (value > target) { value--; updateImage(); } else if (value < target) { value++; updateImage(); } } /** * Add a new score to the current counter value. */ public void add(int score) { target += score; } // you could add this message to the class public void subtract(int score) { target -= score; } /** * Return the current counter value. */ public int getValue() { return value; } /** * Set a new counter value. */ public void setValue(int newValue) { target = newValue; value = newValue; updateImage(); } /** * Update the image on screen to show the current value. */ private void updateImage() { GreenfootImage image = new GreenfootImage(background); GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent); image.drawImage(text, (image.getWidth()-text.getWidth())/2, (image.getHeight()-text.getHeight())/2); setImage(image); } }
code for world:

import greenfoot.*;

/**
 * all history of bobsworld.
 * 
 * @author (A.T.P) 
 * @version (2.4.2)
 */
public class BobWorld extends World
{

    /**
     * Constructor for objects of class BobWorld.
     * 
     */
    public BobWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(700, 500, 1); 

        prepare();
    }

    LifeCounter lifeCounter = new LifeCounter();
    private LifeCounter LifeCounter = new LifeCounter();
    public LifeCounter getLifeCounter()
    {
        return lifeCounter;
    }
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        addObjectBob();
       Bob bob = new Bob();
        addObject(Bob,x1 = 121,y1 =  186);
        addObject(Bob.getLifeCounter(),x2 = 40,y2 = 70);

        Score score = new Score();
        addObject(score,45,15);

        Bob bob = new Bob(score);
        addObject(bob, 121, 186);

        bob.setLocation(94, 251);
        bob.setLocation(106, 232);
        bob.setLocation(67, 185);
        bob.setLocation(72, 162);
        bob.setLocation(76, 177);
        bob.setLocation(75, 177);
        bob.setLocation(75, 175);
        bob.setLocation(85, 204);
        BobFood bobfood = new BobFood();
        addObject(bobfood, 468, 205);
        bob.setLocation(86, 204);
        bob.setLocation(100, 191);
        bob.setLocation(98, 191);
        bob.setLocation(134, 168);
        bob.setLocation(95, 206);
        bobfood.setLocation(451, 210);
        BobEater bobeater = new BobEater();
        addObject(bobeater, 81, 140);
        BobEater bobeater2 = new BobEater();
        addObject(bobeater2, 74, 334);
        bob.setLocation(201, 235);
        bobfood.setLocation(451, 181);
        bob.setLocation(202, 235);
        bob.setLocation(203, 235);
        bob.setLocation(207, 216);
        bobeater.setLocation(84, 142);
        bobeater.setLocation(85, 150);
        bobeater2.setLocation(76, 315);
        BobFood bobfood2 = new BobFood();
        addObject(bobfood2, 465, 46);
        BobFood bobfood3 = new BobFood();
        addObject(bobfood3, 468, 314);
        BobFood bobfood4 = new BobFood();
        addObject(bobfood4, 286, 95);
        BobFood bobfood5 = new BobFood();
        addObject(bobfood5, 306, 289);
        BobFood bobfood6 = new BobFood();
        addObject(bobfood6, 326, 201);
        BobFood bobfood7 = new BobFood();
        addObject(bobfood7, 552, 103);
        BobFood bobfood8 = new BobFood();
        addObject(bobfood8, 541, 263);
        BobFood bobfood9 = new BobFood();
        addObject(bobfood9, 139, 34);
        BobFood bobfood10 = new BobFood();
        addObject(bobfood10, 181, 340);
        BobFood bobfood11 = new BobFood();
        addObject(bobfood11, 69, 213);
        BobFood bobfood12 = new BobFood();
        addObject(bobfood12, 182, 124);
        Instructions instructions = new Instructions();
        addObject(instructions, 272, 395);
        instructions.setLocation(282, 389);
        bobeater2.setLocation(76, 304);
        BobEater bobeater3 = new BobEater();
        addObject(bobeater3, 73, 312);
        BobEater bobeater4 = new BobEater();
        addObject(bobeater4, 389, 100);
        BobEater bobeater5 = new BobEater();
        addObject(bobeater5, 545, 202);
        bobeater3.setLocation(76, 312);
        bobeater3.setLocation(346, 329);
        bobeater3.setLocation(344, 329);
        score.setLocation(44, 15);
        score.setLocation(27, 18);
        score.setLocation(27, 24);
        removeObject(bobfood6);
        removeObject(bobfood);
        removeObject(bobfood9);
        bobfood3.setLocation(469, 313);
        removeObject(bobfood3);
        bobfood4.setLocation(286, 98);
        removeObject(bobfood4);
        bobfood12.setLocation(198, 87);
        bobfood5.setLocation(306, 260);
        bobfood2.setLocation(396, 179);
        bobfood2.setLocation(405, 167);
        bobfood8.setLocation(491, 312);
        bobfood8.setLocation(491, 310);
        bobfood7.setLocation(543, 57);
        bobfood10.setLocation(154, 340);
        bobfood11.setLocation(56, 214);
        bobfood11.setLocation(56, 215);
        bobfood5.setLocation(307, 257);
        bobfood2.setLocation(434, 132);
        bobfood2.setLocation(416, 116);
        bobeater4.setLocation(402, 115);
        bobeater4.setLocation(378, 98);
        bobfood2.setLocation(421, 136);
        bobfood2.setLocation(421, 135);
        instructions.setLocation(361, 489);
        bobfood8.setLocation(573, 404);
        bobfood5.setLocation(326, 395);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(154, 340);
        bobfood10.setLocation(50, 426);
        bobfood11.setLocation(67, 217);
        bobfood2.setLocation(378, 245);
        bobeater3.setLocation(378, 363);
        bobeater2.setLocation(86, 306);
        bobeater.setLocation(154, 80);
        bobeater3.setLocation(381, 411);
        bobeater3.setLocation(384, 430);
        bobeater2.setLocation(110, 341);
        score.setLocation(52, 23);
        score.setLocation(51, 22);
        score.setLocation(50, 16);
    }
}
danpost danpost

2015/7/20

#
The LifeCounter object that the 'getLivesCounter' method of the Bob class changes is not the same LifeCounter object that has been added into your world. I have found at least three occurrences of the expression 'new LifeCounter()' within the classes given. Also, there are two occurrances of 'new Bob' in your BobWorld class. Since you will only have one of each in your world, you should have only one occurrence of each in your code (that means in total among all your classes).
swacks swacks

2015/7/20

#
k wrote...
i found some errors in the code that i never saw i also updated the code
LifeCountercode

import greenfoot.*;
import java.awt.Color;
/**
 * Write a description of class LifeCounter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LifeCounter extends Actor 
{

    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value; 
    private int target;
    /**
     * 
     */
    public LifeCounter()
    {
        background = getImage(); 
        value = 3;
        target = 3;
        updateImage();
    }

    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value > target) {
            value--;
            updateImage();
        }
        else if (value < target) {
            value++;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target += score;
    }

    // you could add this message to the class
    public void subtract(int score)
    {
        target -= score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
            (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }

}

code for Main Character 

import greenfoot.*;

/**
 * a bob in the game will eat the Bobfood on your command.
 * 
 * @author (A.T.P) 
 * @version (2.4.2)
 */
public class Bob extends Beings
{

    private int points = 0;
    private Score Score;
    // instance field to add
    LifeCounter lifeCounter = new LifeCounter();
    // methods to add
   
    public Bob(Score pointScore)
    {
        Score = pointScore;
    }

    /**
     * Act - do whatever the Bob wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(3);
        checkKeys();
        tryToEat();
    }    

    /**
     * once reached world edge you must turn away
     */
    public void turnAtWorldEdge()
    {  if (atWorldEdge())
        {
            turn(4);
        }
    }

    /**
     * check whether the keys are being pressed or not if they are follow the command
     */
    public void checkKeys()
    {
        if( Greenfoot.isKeyDown("left"))
        {
            turn(-6);   
        }
        if( Greenfoot.isKeyDown("right"))
        {
            turn(6);
        }
        if( Greenfoot.isKeyDown("up"))
        {
            move(5);   
        }
        if( Greenfoot.isKeyDown("down"))
        {
            move(-5);
        }
    }

    /**
     * Check whether we can see Bobfood.if we can,eat it.
     */
    void tryToEat()
    {
        if(canSee(BobFood.class))
        {
            eat(BobFood.class);
            Greenfoot.playSound("slurp.wav");
            Score.add(5);
            createNewBobFood();
        }

    }

    /**
     * add new BobFood into the world at a random location
     */
    private void createNewBobFood()
    {
        BobFood newBobFood;
        newBobFood=new BobFood();

        World world;
        world=getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);

        world.addObject(newBobFood,x,y);
    }

    /**
     * get the lifecounter into the game
     */
    private int getLifeCounter()
    {
        LifeCounter life = ((BobWorld)getWorld()).getLifeCounter();  
        LifeCounter.add(-1);  
        if (life.getValue() == 0) gameOver();  
    }

    /**
     * adds the method gameover when that happens a sound is produced
     */
    public void gameOver()
    {
        Greenfoot.playSound("game-over.wav");
        Greenfoot.stop();

    }
}

i found out a error i made it was in the spelling so i changed it a bit and now i come up with an error because of:private int getLifeCounter() { LifeCounter life = ((BobWorld)getWorld()).getLifeCounter(); LifeCounter.add(-1); if (life.getValue() == 0) gameOver(); } where: LifeCounter.add(-1); error:non-static method add(int) cannot be referenced from a static context can you give me the code needed to be added and possibly for the other errors you told me there were
danpost danpost

2015/7/20

#
Line 109 of the Bob class creates a reference variable named 'life' for the LifeCounter object. So, line 10 should be:
life.add(-1);
swacks swacks

2015/7/20

#
when i do this it says <identifier> expected and when you said line 10 did you mean line 20 for that was where the tenth line of coding started can you post the coding im missing it would really help i would appretiate it if you could ;)
swacks swacks

2015/7/20

#
im posting more of my other codding as it may help see where the mistakes could be
bobeater,bobfood,bob are all under the catagory of beings which are under actor i will post now

import greenfoot.*;

/**
 * this adds methods into the scenario that wasnt here before.
 * 
 * @author (A.T.P) 
 * @version (2.4.2)
 */
public class Beings extends Actor
{
    /**
     *adds the method "atWorldEdge" to this greenfoot script
     */
    public boolean atWorldEdge() 
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;

        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }    

    /**
     * adds a nonexisting method to the greenfoot code
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0,clss);
        return actor != null;
    }

    /**
     * adds a nonexisting method to the greenfoot code which is called the method eat
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0,clss);
        if (actor != null) {
            getWorld().removeObject (actor);
        }
    }
    
    public void bobEater(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0,clss);
        if (actor != null) {
            getWorld().removeObject (actor);
        }
    }
    
  
   
}
im posting the original score counter

import greenfoot.*;
import java.awt.Color;
/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;

    /**
     * Create a new Score, initialised to 0.
     */
    public Score()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current Score value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current Score value.
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Set a new Score value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}

danpost danpost

2015/7/20

#
danpost wrote...
Line 109 of the Bob class creates a reference variable named 'life' for the LifeCounter object. So, line 10 should be:
life.add(-1);
Sorry, that was supposed to be '110', not '10' (of the Bob class code given above).
swacks swacks

2015/7/20

#
could you give me some coding in the areas i need just review my code and give me all code that i would need
swacks swacks

2015/7/20

#
just check the coding methods i have maybe i need a method which i dont have maybe its in the coding in "beings" i just dont know what
danpost danpost

2015/7/20

#
You do not need MORE code; you have too much as it is .
danpost wrote...
The LifeCounter object that the 'getLivesCounter' method of the Bob class changes is not the same LifeCounter object that has been added into your world. I have found at least three occurrences of the expression 'new LifeCounter()' within the classes given. Also, there are two occurrances of 'new Bob' in your BobWorld class. Since you will only have one of each in your world, you should have only one occurrence of each in your code (that means in total among all your classes).
swacks swacks

2015/7/20

#
hi there thanks - if i change line 110 so that it now reads like this:
    {
        LifeCounter life = ((BobWorld)getWorld()).getLifeCounter();  
        life.add(-1);
        if (life.getValue() == 0) gameOver();  
    }
I get error missing return statement for the "}" highlighted
swacks swacks

2015/7/20

#
Can you also please explain this a bit clearer for me
The LifeCounter object that the 'getLivesCounter' method of the Bob class changes is not the same LifeCounter object that has been added into your world.
.
danpost danpost

2015/7/20

#
swacks wrote...
I get error missing return statement for the "}" highlighted
This is because you declared the method with:
private int getLivesCounter()
which means that the method must return an 'int' value; but, it does not. You can change the 'int' in that line to 'void' or have the method return an 'int' value. To return the new value of the LifeCounter object, you can place the following line at the end of the method:
return life.getValue();
swacks swacks

2015/7/21

#
thanks for the tips do you think the problem could be that i never put a method getLifeCounter but if i must do i put it in class beings and how would i properly make the method
swacks swacks

2015/8/2

#
i would really apreiciate it if you could help me i am still a beginner in coding and im not yet properly fimiliar with the coding language as well as in explanation it would be nice if u could help ;0
There are more replies on the next page.
1
2