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

2013/1/1

non-static method cannot be referenced from a static context

1
2
ctgreenfoot ctgreenfoot

2013/1/1

#
don't understand what is wrong
 private void eatCrab()
    {
        Actor crab;
        crab = getOneObjectAtOffset (0,0,Crab.class);
        if (crab != null)
        {
           int x = Greenfoot.getRandomNumber(getWorld().getWidth());  
        int y = Greenfoot.getRandomNumber(getWorld().getHeight());  
        Crab.setLocation(x, y); 
        lifeCounter.add(1);
        Greenfoot.playSound ("eating.wav");
        }
    }
danpost danpost

2013/1/1

#
Java is case-sensitive. You have a class called 'Crab' and you named an Actor object 'crab'. On line 9, you are trying to set the location of the class (not the object).
ctgreenfoot ctgreenfoot

2013/1/3

#
I changed this and then this happened.
for (int item=0; item<2; item++)
       addObject (new Lobster(), (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));
        
The 'new Lobster' highlights in red and says ' cannot find symbol - constructor Lobster()'
ctgreenfoot ctgreenfoot

2013/1/3

#
Here is my code for the Lobster:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Lobster here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lobster extends Animals
{
    /**
     * Act - do whatever the Lobster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private LifeCounter lifeCounter;
    
    public Lobster(LifeCounter pointLifeCounter)
    {
        lifeCounter = pointLifeCounter;
    }
    
    public void act() 
    {
       
        moveAround();
        eatCrab();
    }  
    private void moveAround()
    {
        move(5);

        if (Greenfoot.getRandomNumber (100) < 10)
            {
                turn(Greenfoot.getRandomNumber (90-45));
            }
            
        if (atWorldEdge())
        {
            turn(180);
        }
        }
            
        public boolean atWorldEdge()
        {
        if (getX() <= 5 || getX() >= getWorld() . getWidth() -5)
            return true;
        if (getY() <= 5 || getY() >= getWorld() . getHeight() -5)
            return true;
        else
            return false;
        
    }
    private void eatCrab()
    {
        Actor crab;
        crab = getOneObjectAtOffset (0,0,Crab.class);
        if (crab != null)
        {
           int x = Greenfoot.getRandomNumber(getWorld().getWidth());  
        int y = Greenfoot.getRandomNumber(getWorld().getHeight());  
        crab.setLocation(x, y); 
        lifeCounter.add(1);
        Greenfoot.playSound ("eating.wav");
        }
    }
}
danpost danpost

2013/1/3

#
I appears that at the moment you cannot create a lobster without passing a LifeCounter object to it. So, you either need to pass one or create another constructor in the Lobster class that does not pass any parameters.
ctgreenfoot ctgreenfoot

2013/1/4

#
how would i create another constructor in the Lobster class with no parameters?
danpost danpost

2013/1/4

#
public Lobster()
{
}
Just be aware that there will be no LifeCounter object set up for that lobster. In the Lobster class you could change line 16 to
private LifeCounter lifeCounter = null;
and line 63 to
if(lifeCounter!=null) lifeCounter.add(1);
ctgreenfoot ctgreenfoot

2013/1/4

#
ok, now i'm really confused. i did as you said and everything compiles as it should but when I play it is not how I meant it to be. I wanted the lifeCounter to start off with the value of 5 and then lose one life every time it is 'eaten' by the crab, then when the life counter reaches zero the game would stop. What is happening right now is that even though it starts off with 5, it stays that way and the points do not go down as the crab is eaten. Also, the game does not stop after the counter reaches zero. Help! here are my codes:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A simple counter with graphical representation as an actor on screen.
 * 
 * @author mik
 * @version 1.0
 */
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;

    /**
     * Create a new counter, initialised to 0.
     */
    public LifeCounter()
    {
        background = getImage();  // get image from class
        value = 5;
        target = 5;
        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;
    }

    /**
     * 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);
    }
  
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Lobster here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lobster extends Animals
{
    /**
     * Act - do whatever the Lobster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public Lobster()
    {
        
    }
    
    private LifeCounter lifeCounter = null;
    
    public Lobster(LifeCounter pointLifeCounter)
    {
        lifeCounter = pointLifeCounter;
    }
    
    public void act() 
    {
       
        moveAround();
        eatCrab();
    }  
    
    private void moveAround()
    {
        move(5);

        if (Greenfoot.getRandomNumber (100) < 10)
            {
                turn(Greenfoot.getRandomNumber (90-45));
            }
            
        if (atWorldEdge())
        {
            turn(180);
        }
        }
            
        public boolean atWorldEdge()
        {
        if (getX() <= 5 || getX() >= getWorld() . getWidth() -5)
            return true;
        if (getY() <= 5 || getY() >= getWorld() . getHeight() -5)
            return true;
        else
            return false;
        
    }
    private void eatCrab()
    {
        Actor crab;
        crab = getOneObjectAtOffset (0,0,Crab.class);
        if (crab != null)
        {
           int x = Greenfoot.getRandomNumber(getWorld().getWidth());  
        int y = Greenfoot.getRandomNumber(getWorld().getHeight());  
        crab.setLocation(x, y); 
        if(lifeCounter != null) lifeCounter.add(1);
        Greenfoot.playSound ("eating.wav");
        }
    }
}
danpost danpost

2013/1/4

#
You do not yet have any code to stop the game when the lifeCounter value reaches zero. You need to add a conditional 'if' statement at line 71 of the Lobster class to check the lifeCounter value. I created a 'new' scenario and copied your code (with minor 'insignificant' changes) into it and ran it. The counter seemed to me to be working properly. Would you please post your world class, as I believe that there may be a problem there.
ctgreenfoot ctgreenfoot

2013/1/7

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class CrabWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CrabWorld extends World
{



    /**
     * Constructor for objects of class CrabWorld.
     * 
     */
    public CrabWorld()
    {    
            super(1000, 800, 1); 
        
       
        prepareWorld();
        backgroundMusic();
     
    }
    
    
    private void backgroundMusic()
    {
       Greenfoot.playSound("background.mp3");
    }
       
    private void prepareWorld()
    {
        Counter counter = new Counter();
        addObject(counter, 100, 70);
        
        Crab crab = new Crab(counter);
        addObject(crab, 500, 400);
        
        CountdownClock countdownClock = new CountdownClock();
        addObject(countdownClock, 100, 40);
       
        if (counter.getValue() == 20)  
         {  
           RedWorm redWorm = new RedWorm();  
           addObject(redWorm, (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));  
         }  
        
for (int item=0; item<Greenfoot.getRandomNumber(11)+10; item++)
      addObject (new Worm(), (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));
            
for (int item=0; item<2; item++)
       addObject (new Lobster(), (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));

       
    }
}
danpost danpost

2013/1/7

#
If the crab is the user-controlled actor that has life, why are you trying to put counter on the lobster objects? You add a counter to the crab object in the world constructor; that is the only counter you need to track its lives. If I am correct on this count, replace the whole Lobster class code with the following:
ctgreenfoot ctgreenfoot

2013/1/7

#
it says that it cannot find the symbol: 'crab.lifeCounter.add(1);'
danpost danpost

2013/1/7

#
I have no idea what you called the counter in the crab class or what methods you have in the counter class to adjust its values (I just guessed).
ctgreenfoot ctgreenfoot

2013/1/9

#
i dont think that i have the counter in the crab class. should i add it? if so, how?
danpost danpost

2013/1/9

#
Lines 39 and 40 of your world class clearly show you passing a Counter object to the Crab class constructor. Anyway, isn't it the Crab that is supposed to have so many lives and the lobster never dies? Then, the Crab object should be the one with the Counter object, not the lobster at all. If I am correct on this count, you should use the revised Lobster class code I just gave above, which needs one correction. At line 40 (I think), change
crab = getOneObjectAtOffset (0,0,Crab.class);
// TO
crab = (Crab)getOneObjectAtOffset (0,0,Crab.class);
Then, if you have questions about your Crab class, post it.
There are more replies on the next page.
1
2